BGRABitmap tutorial 1

From Lazarus 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 first tutorial shows you how to use BGRABitmap library.

You can download the library on GitHub.

Create a new project

Create an windowed application with menu Project > New project.

The main form unit should look like this:

unit UMain;

{$mode objfpc}{$H+}

interface

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

type

  { TForm1 }

  TForm1 = class(TForm)
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  Form1: TForm1; 

implementation

initialization
  {$I UMain.lrs}

end.

If you do not find it, use Ctrl-F12 to show file list.

Save your project next to BGRABitmap library with menu File > Save all (not necessarily in the same folder).

Add reference to BGRABitmap

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

In the unit clause, add a reference to BGRABitmap and BGRABitmapTypes after Dialogs.

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

Alternative ways of referencing

By installing BGRABitmap package

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

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

By adding BGRABitmap units to the search path

Another 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".

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

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

Add some drawing

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

procedure TForm1.FormPaint(Sender: TObject);
var bmp: TBGRABitmap;
begin
  bmp := TBGRABitmap.Create(ClientWidth, ClientHeight, BGRABlack);
  bmp.FillRect(20, 20, 100, 40, BGRA(255,192,0), dmSet);  //fill an orange rectangle
  bmp.Draw(Canvas, 0, 0, True);                           //render BGRABitmap on the form
  bmp.Free;                                               //free memory
end;

As you can see, you need to define a TBGRABitmap variable and create it. There are several constructors for 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.

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

After that, there is a color parameter with red/green/blue components, and a drawing mode. dmSet means to simply replace the pixels.

Do not forget to free the object after using it, to avoid a memory leak.

Resulting code

You should obtain the following code:

unit UMain;

{$mode objfpc}{$H+}

interface

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

type

  { TForm1 }

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

var
  Form1: TForm1; 

implementation

{ TForm1 }

procedure TForm1.FormPaint(Sender: TObject);
var bmp: TBGRABitmap;
begin
  bmp := TBGRABitmap.Create(ClientWidth, ClientHeight, BGRABlack);
  bmp.FillRect(20, 20, 100, 40, BGRA(255, 192, 0), dmSet);
  bmp.Draw(Canvas, 0, 0, True);
  bmp.Free;
end;

initialization
  {$I UMain.lrs}

end.

Run the program

You should obtain a window filled in black with an orange rectangle in it.

BGRATutorial1.png

Go to next tutorial (image loading)