BGRABitmap tutorial 2

From Free Pascal wiki
Jump to navigationJump to search

Deutsch (de) English (en) español (es) français (fr) русский (ru)


Home | Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 | Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 | Tutorial 11 | Tutorial 12 | Tutorial 13 | Tutorial 14 | Tutorial 15 | Tutorial 16 | Edit

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:

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

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:

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:

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.

BGRATutorial2.png

Center the image

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

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:

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

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

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

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

First tutorial | Next tutorial (drawing with the mouse)