Sign in Join
Wiki copytemporary

BGRABitmap tutorial 1: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11This first tutorial shows you how to use [[BGRABitmap]] library.
22
33You can download the library on [GitHub](https://github.com/bgrabitmap).
44
55### Create a new project
66
77Create an windowed application with menu **Project \> New project**.
88
99The main form unit should look like this:
1010
1111``` pascal
1212unit UMain;
1313
1414{$mode objfpc}{$H+}
1515
1616interface
1717
1818uses
1919 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs;
2020
2121type
2222
2323 { TForm1 }
2424
2525 TForm1 = class(TForm)
2626 private
2727 { private declarations }
2828 public
2929 { public declarations }
3030 end;
3131
3232var
3333 Form1: TForm1;
3434
3535implementation
3636
3737initialization
3838 {$I UMain.lrs}
3939
4040end.
4141```
4242
4343If you do not find it, use **Ctrl-F12** to show file list.
4444
4545Save your project next to BGRABitmap library with menu **File \> Save all** (not necessarily in the same folder).
4646
4747### Add reference to BGRABitmap
4848
4949The first time you use BGRABitmap, open bgrabitmappack.lpk with Lazarus and click on "Use \> Add to Project". Then, if you need to add the reference in another project, you can do it through the Project inspector and click on "Add... \> New condition" and choose BGRABitmapPack.
5050
5151In the unit clause, add a reference to BGRABitmap and BGRABitmapTypes after Dialogs.
5252
5353``` pascal
5454uses
5555 Classes, SysUtils, FileUtil, LResources,
5656 Forms, Controls, Graphics, Dialogs,
5757 BGRABitmap, BGRABitmapTypes;
5858```
5959
6060#### Alternative ways of referencing
6161
6262##### By installing BGRABitmap package
6363
6464Open bgrabitmappack.lpk as a package. Make sure it is possible to install it by going in the package window into "Options \> IDE Integration" and setting the package type to "designtime and runtime". Then in the package window, click on install. A dialog pops up, asking if the package must be added to Lazarus and if it needs to be compiled again. Choose Yes twice.
6565
6666If everything is fine, Lazarus restarts and BGRABitmap units are available. If it does not work, you can simply add BGRABitmap to the search path without compiling Lazarus.
6767
6868##### By adding BGRABitmap units to the search path
6969
7070Another way is to add BGRABitmap units to the search path of the project. To do this, go to compiler options with menu **Project \> Compiler options**. In other unit files path, add the relative path to BGRABitmap. For example, if BGRABitmap is in a folder next to your project, the relative path could be "..\BGRABitmap".
7171
7272If you copy BGRABitmap files in the same folder as your project, then you do not need to add such search path. However, it is not recommended because if you have multiple projects using the library, it could become a repetitive task to update to a new version of the library.
7373
7474If you are lost with relative path, you can also add the relative path by adding the BGRABitmap unit to your project. To do so, open within your project the file bgrabitmap.pas. Then use menu **Project \> Add file to project**. Lazarus will ask if you want to add the file and the new directory to the project.
7575
7676### Add some drawing
7777
7878Add a painting event. To do this, click on the form, then go to the object inspector, in the event tab, and double click on the OnPaint line. Lazarus will add automatically a FormPaint handler to the main form unit. Add for example the following code inside it:
7979
8080``` pascal
8181procedure TForm1.FormPaint(Sender: TObject);
8282var bmp: TBGRABitmap;
8383begin
8484 bmp := TBGRABitmap.Create(ClientWidth, ClientHeight, BGRABlack);
8585 bmp.FillRect(20, 20, 100, 40, BGRA(255,192,0), dmSet); //fill an orange rectangle
8686 bmp.Draw(Canvas, 0, 0, True); //render BGRABitmap on the form
8787 bmp.Free; //free memory
8888end;
8989```
9090
9191As you can see, you need to define a [[TBGRABitmap class|TBGRABitmap]] variable and create it. There are several constructors for [[TBGRABitmap class|TBGRABitmap]]. The one used here creates a bitmap of size ClientWidth x ClientHeight and filled with black. ClientWidth and ClientHeight are form properties that return the available space for drawing inside the form.
9292
9393The FillRect procedure takes usual parameters for drawing a rectangle, that is the upper-left corner followed by the lower-right corner plus 1. It means that the pixel at (100,40) is excluded from the rectangle.
9494
9595After that, there is a color parameter with red/green/blue components, and a drawing mode. dmSet means to simply replace the pixels.
9696
9797Do not forget to free the object after using it, to avoid a memory leak.
9898
9999### Resulting code
100100
101101You should obtain the following code:
102102
103103``` pascal
104104unit UMain;
105105
106106{$mode objfpc}{$H+}
107107
108108interface
109109
110110uses
111111 Classes, SysUtils, FileUtil, LResources,
112112 Forms, Controls, Graphics, Dialogs,
113113 BGRABitmap, BGRABitmapTypes;
114114
115115type
116116
117117 { TForm1 }
118118
119119 TForm1 = class(TForm)
120120 procedure FormPaint(Sender: TObject);
121121 private
122122 { private declarations }
123123 public
124124 { public declarations }
125125 end;
126126
127127var
128128 Form1: TForm1;
129129
130130implementation
131131
132132{ TForm1 }
133133
134134procedure TForm1.FormPaint(Sender: TObject);
135135var bmp: TBGRABitmap;
136136begin
137137 bmp := TBGRABitmap.Create(ClientWidth, ClientHeight, BGRABlack);
138138 bmp.FillRect(20, 20, 100, 40, BGRA(255, 192, 0), dmSet);
139139 bmp.Draw(Canvas, 0, 0, True);
140140 bmp.Free;
141141end;
142142
143143initialization
144144 {$I UMain.lrs}
145145
146146end.
147147```
148148
149149### Run the program
150150
151151You should obtain a window filled in black with an orange rectangle in it.
152152
153-<figure>
154-<img src="BGRATutorial1.png" title="BGRATutorial1.png" />
155-<figcaption>BGRATutorial1.png</figcaption>
156-</figure>
153+![](/wiki-images/e/ea/BGRATutorial1.png)
157154
158155[[BGRABitmap tutorial 2|Go to next tutorial (image loading)]]
159156
160157---
161158*From the Free Pascal wiki, page "BGRABitmap tutorial 1", as it stood on 2020-02-11 after a change by Andry, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
162159Category: BGRABitmap
163160Category: Graphics

Back to history