Sign in Join
Wiki copytemporary

BGRABitmap tutorial 2: what changed

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

OldNewLine
11This tutorial shows you how to load an image and draw it on a form.
22
33### Create a new project
44
55Create a new project and add a reference to [[BGRABitmap]], the same way as in [[BGRABitmap tutorial|the first tutorial]].
66
77### Load the bitmap
88
99Copy an image into your project directory. Let's suppose it's name is *image.png*.
1010
1111Add a private variable to the main form to store the image:
1212
1313``` pascal
1414TForm1 = class(TForm)
1515 private
1616 { private declarations }
1717 image: TBGRABitmap;
1818 public
1919 { public declarations }
2020 end;
2121```
2222
2323Load the image when the form is created. To do this, double-click on the form, a procedure should appear in the code editor. Add the following [[TBGRACustomBitmap and IBGRAScanner|loading instruction]] (and don't forget to destroy the image at the end):
2424
2525``` pascal
2626procedure TForm1.FormCreate(Sender: TObject);
2727begin
2828 image := TBGRABitmap.Create('image.png');
2929end;
3030
3131procedure TForm1.FormDestroy(Sender: TObject);
3232begin
3333 image.Free;
3434end;
3535```
3636
3737### Draw the bitmap
3838
3939Add an OnPaint handler. To do this, select the main form, then go to the object inspector, in the event tab, and double-click on the OnPaint line. Then, add the drawing code:
4040
4141``` pascal
4242procedure TForm1.FormPaint(Sender: TObject);
4343begin
4444 image.Draw(Canvas,0,0,True);
4545end;
4646```
4747
4848Notice that the last parameter is set to True, which means opaque. If you want to take transparent pixels into account, encoded in the alpha channel, you must use False instead. But it can be slow to use transparent drawing on standard canvas, so if it is not necessary, use opaque drawing only.
4949
5050### Code
5151
5252Finally you should have something like:
5353
5454``` pascal
5555unit UMain;
5656
5757{$mode objfpc}{$H+}
5858
5959interface
6060
6161uses
6262 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
6363 BGRABitmap, BGRABitmapTypes;
6464
6565type
6666 { TForm1 }
6767
6868 TForm1 = class(TForm)
6969 procedure FormCreate(Sender: TObject);
7070 procedure FormDestroy(Sender: TObject);
7171 procedure FormPaint(Sender: TObject);
7272 private
7373 { private declarations }
7474 image: TBGRABitmap;
7575 public
7676 { public declarations }
7777 end;
7878
7979var
8080 Form1: TForm1;
8181
8282implementation
8383
8484{ TForm1 }
8585
8686procedure TForm1.FormCreate(Sender: TObject);
8787begin
8888 image := TBGRABitmap.Create('image.png');
8989end;
9090
9191procedure TForm1.FormDestroy(Sender: TObject);
9292begin
9393 image.free;
9494end;
9595
9696procedure TForm1.FormPaint(Sender: TObject);
9797begin
9898 image.Draw(Canvas,0,0,True);
9999end;
100100
101101initialization
102102 {$I UMain.lrs}
103103
104104end.
105105```
106106
107107### Run the program
108108
109109You should see a form with an image drawn in it at the upper-left corner.
110110
111-<figure>
112-<img src="BGRATutorial2.png" title="BGRATutorial2.png" />
113-<figcaption>BGRATutorial2.png</figcaption>
114-</figure>
111+![](/wiki-images/d/db/BGRATutorial2.png)
115112
116113### Center the image
117114
118115You may want to center the image on the form. To do this, modify the FormPaint procedure:
119116
120117``` pascal
121118procedure TForm1.FormPaint(Sender: TObject);
122119var ImagePos: TPoint;
123120begin
124121 ImagePos := Point( (ClientWidth - Image.Width) div 2,
125122 (ClientHeight - Image.Height) div 2 );
126123
127124 // test for negative position
128125 if ImagePos.X < 0 then ImagePos.X := 0;
129126 if ImagePos.Y < 0 then ImagePos.Y := 0;
130127
131128 image.Draw(Canvas,ImagePos.X,ImagePos.Y,True);
132129end;
133130```
134131
135132To compute the position, we need to calculate the space between the image and the left border (X coordinate) and the space between the image and the top border (Y coordinate). The expression ClientWidth - Image.Width returns the available horizontal space, and we divide it by 2 to obtain the left margin.
136133
137134The result can be negative if the image is bigger than the client width. In this case, the margin is just set to zero.
138135
139136You can run the program and see if it works. Notice what happens if we remove the test for negative position.
140137
141138### Stretch the image
142139
143140To stretch the image, we need to create a temporary stretched image:
144141
145142``` pascal
146143procedure TForm1.FormPaint(Sender: TObject);
147144var stretched: TBGRABitmap;
148145begin
149146 stretched := image.Resample(ClientWidth, ClientHeight) as TBGRABitmap;
150147 stretched.Draw(Canvas,0,0,True);
151148 stretched.Free;
152149end;
153150```
154151
155152By default, it uses fine resample, but you can precise if you want to use simple stretch instead (faster):
156153
157154``` pascal
158155stretched := image.Resample(ClientWidth, ClientHeight, rmSimpleStretch) as TBGRABitmap;
159156```
160157
161158You can also specify the interpolation filter with the [[TBGRACustomBitmap and IBGRAScanner|ResampleFilter]] property:
162159
163160``` pascal
164161image.ResampleFilter := rfMitchell;
165162stretched := image.Resample(ClientWidth, ClientHeight) as TBGRABitmap;
166163```
167164
168165[[BGRABitmap tutorial 1|First tutorial]] \| [[BGRABitmap tutorial 3|Next tutorial (drawing with the mouse)]]
169166
170167---
171168*From the Free Pascal wiki, page "BGRABitmap tutorial 2", as it stood on 2023-02-13 after a change by Wp, 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).*
172169Category: BGRABitmap
173170Category: Graphics

Back to history