Sign in Join
Wiki copytemporary

BGRABitmap tutorial 2

Deutsch · English · Español · Français · Русский

This tutorial shows you how to load an image and draw it on a form.

Create a new project

Create a new project and add a reference to BGRABitmap, the same way as in the first tutorial.

Load the bitmap

Copy an image into your project directory. Let's suppose it's name is image.png.

Add a private variable to the main form to store the image:

pascal
TForm1 = class(TForm)
  private
    { private declarations }
    image: TBGRABitmap;
  public
    { public declarations }
  end;

Load 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 loading instruction (and don't forget to destroy the image at the end):

pascal
procedure TForm1.FormCreate(Sender: TObject);
begin
  image := TBGRABitmap.Create('image.png');
end; 

procedure TForm1.FormDestroy(Sender: TObject);
begin
  image.Free;
end;

Draw the bitmap

Add 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:

pascal
procedure TForm1.FormPaint(Sender: TObject);
begin
  image.Draw(Canvas,0,0,True);
end;

Notice 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.

Code

Finally you should have something like:

pascal
unit UMain;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  BGRABitmap, BGRABitmapTypes;

type
  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  private
    { private declarations }
    image: TBGRABitmap;
  public
    { public declarations }
  end; 

var
  Form1: TForm1; 

implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  image := TBGRABitmap.Create('image.png');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  image.free;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  image.Draw(Canvas,0,0,True);
end;

initialization
  {$I UMain.lrs}

end.

Run the program

You should see a form with an image drawn in it at the upper-left corner.

Center the image

You may want to center the image on the form. To do this, modify the FormPaint procedure:

pascal
procedure TForm1.FormPaint(Sender: TObject);
var ImagePos: TPoint;
begin
  ImagePos := Point( (ClientWidth - Image.Width) div 2,
                     (ClientHeight - Image.Height) div 2 );

  // test for negative position
  if ImagePos.X < 0 then ImagePos.X := 0;
  if ImagePos.Y < 0 then ImagePos.Y := 0;

  image.Draw(Canvas,ImagePos.X,ImagePos.Y,True);
end;

To 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.

The result can be negative if the image is bigger than the client width. In this case, the margin is just set to zero.

You can run the program and see if it works. Notice what happens if we remove the test for negative position.

Stretch the image

To stretch the image, we need to create a temporary stretched image:

pascal
procedure TForm1.FormPaint(Sender: TObject);
var stretched: TBGRABitmap;
begin
  stretched := image.Resample(ClientWidth, ClientHeight) as TBGRABitmap;
  stretched.Draw(Canvas,0,0,True);
  stretched.Free;
end;

By default, it uses fine resample, but you can precise if you want to use simple stretch instead (faster):

pascal
stretched := image.Resample(ClientWidth, ClientHeight, rmSimpleStretch) as TBGRABitmap;

You can also specify the interpolation filter with the ResampleFilter property:

pascal
image.ResampleFilter := rfMitchell;
stretched := image.Resample(ClientWidth, ClientHeight) as TBGRABitmap;

First tutorial | Next tutorial (drawing with the mouse)


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.

Revision 2, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.