1 module rawbitmap;
2 
3 import dguihub;
4 
5 class MainForm : Form {
6    private PictureBox _pict;
7    private Bitmap _orgBmp;
8    private Bitmap _bmp;
9 
10    public this() {
11       this._orgBmp = Bitmap.fromFile("image.bmp"); //Load the bitmap from file (this is the original)
12       this._bmp = Bitmap.fromFile("image.bmp"); //Load the bitmap from file (this one will be modified)
13 
14       this.text = "DGui Events";
15       this.size = Size(300, 250);
16       this.startPosition = FormStartPosition.centerScreen; // Set Form Position
17 
18       this._pict = new PictureBox();
19       this._pict.sizeMode = SizeMode.autoSize; // Stretch the image
20       this._pict.dock = DockStyle.fill; // Fill the whole form area
21       this._pict.image = this._bmp;
22       this._pict.parent = this;
23 
24       this.menu = new MenuBar();
25       MenuItem mi = this.menu.addItem("Bitmap");
26 
27       MenuItem mOrgColors = mi.addItem("Original Colors");
28       MenuItem mInvColors = mi.addItem("Invert Colors");
29       MenuItem mGsColors = mi.addItem("Gray Scale");
30 
31       mOrgColors.click.attach(&this.onMenuOrgColorsClick);
32       mInvColors.click.attach(&this.onMenuInvColorsClick);
33       mGsColors.click.attach(&this.onMenuGsColorsClick);
34    }
35 
36    private void onMenuOrgColorsClick(MenuItem sender, EventArgs e) {
37       BitmapData bd;
38       this._orgBmp.getData(bd); //Get the original data
39       this._bmp.setData(bd); //Set the original bitmap data
40       this._pict.invalidate(); // Tell at the PictureBox to redraw itself
41    }
42 
43    private void onMenuInvColorsClick(MenuItem sender, EventArgs e) {
44       BitmapData bd;
45       this._bmp.getData(bd); //Get the original data
46 
47       for (int i = 0; i < bd.bitsCount; i++) // Invert Colors!
48       {
49          bd.bits[i].rgbRed = ~bd.bits[i].rgbRed;
50          bd.bits[i].rgbGreen = ~bd.bits[i].rgbGreen;
51          bd.bits[i].rgbBlue = ~bd.bits[i].rgbBlue;
52       }
53 
54       this._bmp.setData(bd); //Set the original bitmap data
55       this._pict.invalidate(); // Tell at the PictureBox to redraw itself
56    }
57 
58    private void onMenuGsColorsClick(MenuItem sender, EventArgs e) {
59       BitmapData bd;
60       this._bmp.getData(bd); //Get the original data
61 
62       for (int i = 0; i < bd.bitsCount; i++) // Gray Scale!
63       {
64          ubyte mid = cast(ubyte)((bd.bits[i].rgbRed + bd.bits[i].rgbGreen + bd.bits[i].rgbBlue) / 3);
65 
66          bd.bits[i].rgbRed = mid;
67          bd.bits[i].rgbGreen = mid;
68          bd.bits[i].rgbBlue = mid;
69       }
70 
71       this._bmp.setData(bd); //Set the original bitmap data
72       this._pict.invalidate(); // Tell at the PictureBox to redraw itself
73    }
74 }
75 
76 int main(string[] args) {
77    return Application.run(new MainForm()); // Start the application
78 }