Difference between revisions of "Developing with Graphics"

From Lazarus wiki
Jump to navigationJump to search
(→‎Introduction to the Graphics model of the LCL: grammar/orthography ("it's descendent" -> "its descendant"))
m (Taking a screenshot of the screen: Fix missing HWND argument of ReleaseDC)
Line 183: Line 183:
   ScreenDC := GetDC(0);
   ScreenDC := GetDC(0);

Revision as of 16:42, 16 February 2012

Deutsch (de) English (en) español (es) français (fr) italiano (it) 日本語 (ja) 한국어 (ko) Nederlands (nl) português (pt) русский (ru) slovenčina (sk) 中文(中国大陆)‎ (zh_CN) 中文(台灣)‎ (zh_TW)

This page describes the basic classes and techniques regarding drawing graphics with Lazarus. Other more specific topics are in separate articles.


Graphics libraries - here you can see the main graphic libraries you can use to develop.

Other graphics articles

2D drawing

  • ZenGL - cross-platform game development library using OpenGL.
  • BGRABitmap - Drawing shapes and bitmaps with transparency, direct access to pixels, etc.
  • LazRGBGraphics - A package for fast in memory image processing and pixel manipulations (like scan line).
  • fpvectorial - Offers support to read, modify and write vectorial images.
  • Double Gradient - Draw 'double gradient' & 'n gradient' bitmaps easy.
  • Gradient Filler - TGradientFiller is the best way to create custom n gradients in Lazarus.
  • PascalMagick - an easy to use API for interfacing with ImageMagick, a multiplatform free software suite to create, edit, and compose bitmap images.
  • Sample Graphics - graphics gallery created with Lazarus and drawing tools
  • Fast direct pixel access - speed comparison of some methods for direct bitmap pixel access

3D drawing


  • TAChart - Charting component for Lazarus
  • PlotPanel - A plotting and charting component for animated graphs
  • Perlin Noise - An article about using Perlin Noise on LCL applications.

Introduction to the Graphics model of the LCL

The LCL provides two kinds of drawing class: Native classes and non-native classes. Native graphics classes are the most traditional way of drawing graphics in the LCL and are also the most important one, while the non-native classes are complementary, but also very important. The native classes are mostly located in the unit Graphics of the LCL and are the well known classes: TBitmap, TCanvas, TFont, TBrush, TPen, TPortableNetworkGraphic, etc.

TCanvas is a class capable of executing drawings. It cannot exist alone and must either be attached to something visible (or at least which may possibly be visible), such as a visual control descending from TControl, or be attached to an off-screen buffer from a TRasterImage descendent (TBitmap is the most commonly used). TFont, TBrush and TPen describe how the drawing of various operations will be executed in the Canvas.

TRasterImage (usually used via its descendant TBitmap) is a memory area reserved for drawing graphics, but it is created for maximum compatibility with the native Canvas and therefore in LCL-Gtk2 in X11 it is located in the X11 server, which makes pixel access via the Pixels property extremely slow. In Windows it is very fast because Windows allows creating a locally allocated image which can receive drawings from a Windows Canvas.

Besides these there are also non-native drawing classes located in the units graphtype (TRawImage), intfgraphics (TLazIntfImage) and lazcanvas (TLazCanvas, this one exists in Lazarus 0.9.31+). TRawImage is the storage and description of a memory area which contains an image. TLazIntfImage is an image which attaches itself to a TRawImage and takes care of converting between TFPColor and the real pixel format of the TRawImage. TLazCanvas is a non-native Canvas which can draw to an image in a TLazIntfImage.

The main difference between the native classes and the non-native ones is that the native ones do not perform exactly the same in all platforms, because the drawing is done by the underlying platform itself. The speed and also the exact final result of the image drawing can have differences. The non-native classes are guaranteed to perform exactly the same drawing in all platforms with a pixel level precision and they all perform reasonably fast in all platforms.

In the widgetset LCL-CustomDrawn the native classes are implemented using the non-native ones.

All of these classes will be better described in the sections below.

Working with TCanvas

Using the default GUI font

This can be done with this simple code:

<delphi>SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT));</delphi>

Drawing a text limited on the width

Use the DrawText routine, first with DT_CALCRECT and then without it.

<delphi>// First calculate the text size then draw it TextBox := Rect(0, currentPos.Y, Width, High(Integer)); DrawText(ACanvas.Handle, PChar(Text), Length(Text),


DrawText(ACanvas.Handle, PChar(Text), Length(Text),

 TextBox, DT_WORDBREAK or DT_INTERNAL);</delphi>

Drawing text with sharp edges (non antialiased)

Some widgetsets support this via

<Delphi>Canvas.Font.Quality := fqNonAntialiased;</Delphi>

Some widgetsets like the gtk2 do not support this and always paint antialiased. Here is a simple procedure to draw text with sharp edges under gtk2. It does not consider all cases, but it should give an idea:

<Delphi>procedure PaintAliased(Canvas: TCanvas; x,y: integer; const TheText: string); var

 w,h: integer;
 IntfImg: TLazIntfImage;
 Img: TBitmap;
 dy: Integer;
 dx: Integer;
 col: TFPColor;
 FontColor: TColor;
 c: TColor;


 if (w<=0) or (h<=0) then exit;
   // paint text to a bitmap
   // get memory image
   // replace gray pixels
   for dy:=0 to h-1 do begin
     for dx:=0 to w-1 do begin
       if c<>FontColor then
   // create bitmap
   // paint


Working with TBitmap

The TBitmap object stores a bitmap where you can draw before showing it to the screen. When you create a bitmap, you must specify the height and width, otherwise it will be zero and nothing will be drawn.

Direct pixel access

To do directly access the pixels of bitmaps one can either use external libraries, such as BGRABitmap, LazRGBGraphics and Graphics32 or use the Lazarus native TLazIntfImage. For a comparison of pixel access methods, see fast direct pixel access.

On some Lazarus widgetsets (notably LCL-Gtk2), the bitmap data is not stored in memory location which can be accessed by the application and in general the LCL native interfaces draw only through native Canvas routines, so each SetPixel / GetPixel operation involves a slow call to the native Canvas API. In LCL-CustomDrawn this is not the case since the bitmap is locally stored for all backends and SetPixel / GetPixel is fast. For obtaining a solution which works in all widgetsets one should use TLazIntfImage. As Lazarus is meant to be platform independent and work in gtk2, the TBitmap class does not provide a property like Scanline. There is a GetDataLineStart function, equivalent to Scanline, but only available for memory images like TLazIntfImage which internally uses TRawImage.

To sum it up, with the standard TBitmap, you can only change pixels indirectly, by using TCanvas.Pixels. Calling a native API to draw / read an individual pixel is course slower than direct pixel access, notably so in LCL-gtk2 and LCL-Carbon.

Drawing color transparent bitmaps

A new feature, implemented on Lazarus 0.9.11, is color transparent bitmaps. Bitmap files (*.BMP) cannot store any information about transparency, but they can work as they had if you select a color on them to represent the transparent area. This is a common trick used on Win32 applications.

The following example loads a bitmap from a Windows resource, selects a color to be transparent (clFuchsia) and then draws it to a canvas.

<delphi>procedure MyForm.MyButtonOnClick(Sender: TObject); var

 buffer: THandle;
 bmp: TBitmap;
 memstream: TMemoryStream;


 bmp := TBitmap.Create;
 buffer := Windows.LoadBitmap(hInstance, MAKEINTRESOURCE(ResourceID));
 if (buffer = 0) then exit; // Error loading the bitmap
 bmp.Handle := buffer;
 memstream := TMemoryStream.create;
   memstream.position := 0;
 bmp.Transparent := True;
 bmp.TransparentColor := clFuchsia;
 MyCanvas.Draw(0, 0, bmp);
 bmp.Free; // Release allocated resource


Notice the memory operations performed with the TMemoryStream. They are necessary to ensure the correct loading of the image.

Taking a screenshot of the screen

Since Lazarus 0.9.16 you can use LCL to take screenshots of the screen on a cross-platform way. The following example code does it (works on gtk2 and win32, but not gtk1 currently):

<delphi>uses Graphics, LCLIntf, LCLType;



 MyBitmap: TBitmap;
 ScreenDC: HDC;


 MyBitmap := TBitmap.Create;
 ScreenDC := GetDC(0);

Working with TLazIntfImage, TRawImage and TLazCanvas

TLazIntfImage is a non-native equivalent of TRasterImage (more commonly utilized in the form of it's descendent TBitmap). The first thing to be aware about this class is that unlike TBitmap it will not automatically allocate a memory area for the bitmap, one should first initialize a memory area and then give it to the TLazIntfImage. Right after creating a TLazIntfImage one should either connect it to a TRawImage or load it from a TBitmap.

TRawImage is of the type object and therefore does not need to be created nor freed. It can either allocate the image memory itself when one calls TRawImage.CreateData or one can pass a memory block allocated for examply by a 3rd party library such as the Windows API of the Cocoa Framework from Mac OS X and pass the information of the image in TRawImage.Description, TRawImage.Data and TRawImage.DataSize. Instead of attaching it to a RawImage one could also load it from a TBitmap which will copy the data from the TBitmap and won't be syncronized with it afterwards. The TLazCanvas cannot exist alone and must always be attached to a TLazIntfImage.

The example below shows how to choose a format for the data and ask the TRawImage to create it for us and then we attach it to a TLazIntfImage and then attach a TLazCanvas to it:

<delphi> uses graphtype, intfgraphics, lazcanvas;


 AImage: TLazIntfImage;
 ACanvas: TLazCanvas;
 lRawImage: TRawImage;


   lRawImage.Description.Init_BPP32_A8R8G8B8_BIO_TTB(AWidth, AHeight);
   AImage := TLazIntfImage.Create(AWidth, AHeight);
   ACanvas := TLazCanvas.Create(AImage);


Initializing a TLazIntfImage

One cannot simply create an instance of TLazIntfImage and start using it. It needs to add a storage to it. There are 3 ways to do this:

1> Attach it to a TRawImage

2> Load it from a TBitmap. Note that it will copy the memory of the TBitmap so it won't remain connected to it.




3> Load it from a raw image description, like this:


  IntfImg := TLazIntfImage.Create(0,0);


Fading example

A fading example with TLazIntfImage

<delphi>{ This code has been taken from the $LazarusPath/examples/lazintfimage/fadein1.lpi project. } uses LCLType, // HBitmap type

    IntfGraphics, // TLazIntfImage type
    fpImage; // TFPColor type


procedure TForm1.FadeIn(ABitMap: TBitMap);
  SrcIntfImg, TempIntfImg: TLazIntfImage;
  ImgHandle,ImgMaskHandle: HBitmap;
  FadeStep: Integer;
  px, py: Integer;
  CurColor: TFPColor;
  TempBitmap: TBitmap;
  for FadeStep:=1 to 32 do begin
    for py:=0 to SrcIntfImg.Height-1 do begin
      for px:=0 to SrcIntfImg.Width-1 do begin
        CurColor.Red:=(CurColor.Red*FadeStep) shr 5;
        CurColor.Green:=(CurColor.Green*FadeStep) shr 5;
        CurColor.Blue:=(CurColor.Blue*FadeStep) shr 5;

Image format specific example

If you know that the TBitmap is using blue 8bit, green 8bit, red 8bit you can directly access the bytes, which is somewhat faster:

<delphi>uses LCLType, // HBitmap type

    IntfGraphics, // TLazIntfImage type
    fpImage; // TFPColor type

... type

 TRGBTripleArray = array[0..32767] of TRGBTriple;
 PRGBTripleArray = ^TRGBTripleArray;

procedure TForm1.FadeIn2(aBitMap: TBitMap);

  IntfImg1, IntfImg2: TLazIntfImage;
  ImgHandle,ImgMaskHandle: HBitmap;
  FadeStep: Integer;
  px, py: Integer;
  CurColor: TFPColor;
  TempBitmap: TBitmap;
  Row1, Row2: PRGBTripleArray;
  //with Scanline-like
  for FadeStep:=1 to 32 do begin
    for py:=0 to IntfImg1.Height-1 do begin
      Row1 := IntfImg1.GetDataLineStart(py); //like Delphi TBitMap.ScanLine
      Row2 := IntfImg2.GetDataLineStart(py); //like Delphi TBitMap.ScanLine
      for px:=0 to IntfImg1.Width-1 do begin
        Row2^[px].rgbtRed:= (FadeStep * Row1^[px].rgbtRed) shr 5;
        Row2^[px].rgbtGreen := (FadeStep * Row1^[px].rgbtGreen) shr 5; // Fading
        Row2^[px].rgbtBlue := (FadeStep * Row1^[px].rgbtBlue) shr 5;

Conversion between TLazIntfImage and TBitmap

Since Lazarus has no TBitmap.ScanLines property, the best way to access the pixels of an image in a fast way for both reading and writing is by using TLazIntfImage. The TBitmap can be converted to a TLazIntfImage by using TBitmap.CreateIntfImage() and after modifying the pixels it can be converted back to a TBitmap by using TBitmap.LoadFromIntfImage(); Here's the sample on how to create TLazIntfImage from TBitmap, modify it and then go back to the TBitmap.


 ...GraphType, IntfGraphics, LCLType, LCLProc,  LCLIntf ...

procedure TForm1.Button4Click(Sender: TObject); var

 b: TBitmap;
 t: TLazIntfImage;


 b := TBitmap.Create;
   t := b.CreateIntfImage;
   // Read and/or write to the pixels
   t.Colors[10,20] := colGreen;


Using the non-native StretchDraw from LazCanvas

Just like TCanvas.StretchDraw there is TLazCanvas.StretchDraw but you need to specify the interpolation which you desire to use. The interpolation which provides a Windows-like StretchDraw with a very sharp result (the opposite of anti-aliased) can be added with: TLazCanvas.Interpolation := TFPSharpInterpolation.Create;

There are other interpolations available in the unit fpcanvas.

<delphi> uses intfgraphics, lazcanvas;

procedure TForm1.StretchDrawBitmapToBitmap(SourceBitmap, DestBitmap: TBitmap; DestWidth, DestHeight: integer); var

 DestIntfImage, SourceIntfImage: TLazIntfImage;
 DestCanvas: TLazCanvas;


 // Prepare the destination
 DestIntfImage := TLazIntfImage.Create(0, 0);
 DestIntfImage.LoadFromBitmap(DestBitmap.Handle, 0);
 DestCanvas := TLazCanvas.Create(DestIntfImage);
 //Prepare the source
 SourceIntfImage := TLazIntfImage.Create(0, 0);
 SourceIntfImage.LoadFromBitmap(SourceBitmap.Handle, 0);
 // Execute the stretch draw via TFPSharpInterpolation
 DestCanvas.Interpolation := TFPSharpInterpolation.Create;
 DestCanvas.StretchDraw(0, 0, DestWidth, DestHeight, SourceIntfImage);
 // Reload the image into the TBitmap


procedure TForm1.FormPaint(Sender: TObject); var

 Bmp, DestBitmap: TBitmap;


 // Prepare the destination
 DestBitmap := TBitmap.Create;
 DestBitmap.Width := 100;
 DestBitmap.Height := 100;
 Bmp := TBitmap.Create;
 Bmp.Width := 10;
 Bmp.Height := 10;
 Bmp.Canvas.Pen.Color := clYellow;
 Bmp.Canvas.Brush.Color := clYellow;
 Bmp.Canvas.Rectangle(0, 0, 10, 10);
 StretchDrawBitmapToBitmap(Bmp, DestBitmap, 100, 100);
 Canvas.Draw(0, 0, Bmp);
 Canvas.Draw(100, 100, DestBitmap);

end; </delphi>

Motion Graphics - How to Avoid flickering

Many programs draw their output to the GUI as 2D graphics. If those graphics need to change quickly you will soon face a problem: quickly changing graphics often flicker on the screen. This happens when users sometimes sees the whole images and sometimes only when it is partially drawn. It occurs because the painting process requires time.

But how can I avoid the flickering and get the best drawing speed? Of course you could work with hardware acceleration using OpenGL, but this approach is quite heavy for small programs or old computers. This tutorial will focus on drawing to a TCanvas. If you need help with OpenGL, take a look at the example that comes with Lazarus. You can also use A.J. Venter's gamepack, which provides a double-buffered canvas and a sprite component.

A brief and very helpful article on avoiding flicker can be found at http://delphi.about.com/library/bluc/text/uc052102g.htm. Although written for Delphi, the techniques work well with Lazarus.

Now we will examine the options we have for drawing to a Canvas:

Draw to a TImage

A TImage consists of 2 parts: A TGraphic, usually a TBitmap, holding the persistent picture and the visual area, which is repainted on every OnPaint. Resizing the TImage does not resize the bitmap. The graphic (or bitmap) is accessible via Image1.Picture.Graphic (or Image1.Picture.Bitmap). The canvas is Image1.Picture.Bitmap.Canvas. The canvas of the visual area of a TImage is only accessible during Image1.OnPaint via Image1.Canvas.

Important: Never use the OnPaint of the Image1 event to draw to the graphic/bitmap of a TImage. The graphic of a TImage is buffered so all you need to do is draw to it from anywhere and the change is there forever. However, if you are constantly redrawing, the image will flicker. In this case you can try the other options. Drawing to a TImage is considered slower then the other approaches.

Resizing the bitmap of a TImage

Note: Do not use this during OnPaint.

<delphi>with Image1.Picture.Bitmap do begin



Same in one step:

<delphi>with Image1.Picture.Bitmap do begin

 SetSize(100, 120);


Painting on the bitmap of a TImage

Note: Do not use this during OnPaint.

<delphi>with Image1.Picture.Bitmap.Canvas do begin

 // fill the entire bitmap with red
 Brush.Color := clRed;
 FillRect(0, 0, Width, Height);


Note: Inside of Image1.OnPaint the Image1.Canvas points to the volatile visible area. Outside of Image1.OnPaint the Image1.Canvas points to Image1.Picture.Bitmap.Canvas.

Another example:

<delphi>procedure TForm1.BitBtn1Click(Sender: TObject); var

 x, y: Integer;


 // Draws the backgroung
 MyImage.Canvas.Pen.Color := clWhite;
 MyImage.Canvas.Rectangle(0, 0, Image.Width, Image.Height);
 // Draws squares
 MyImage.Canvas.Pen.Color := clBlack;
 for x := 1 to 8 do
   for y := 1 to 8 do
     MyImage.Canvas.Rectangle(Round((x - 1) * Image.Width / 8), Round((y - 1) * Image.Height / 8),
       Round(x * Image.Width / 8), Round(y * Image.Height / 8));


Painting on the volatile visual area of the TImage

You can only paint on this area during OnPaint. OnPaint is eventually called automatically by the LCL when the area was invalidated. You can invalidate the area manually with Image1.Invalidate. This will not immediately call OnPaint and you can call Invalidate as many times as you want.

<delphi>procedure TForm.Image1Paint(Sender: TObject); begin

 // paint a line
 Canvas.Pen.Color := clRed;
 Canvas.Line(0, 0, Width, Height);


Draw on the OnPaint event

In this case all the drawing has to be done on the OnPaint event of the form, or of another control. The drawing isn't buffered like in the TImage, and it needs to be fully redrawn in each call of the OnPaint event handler.

<delphi>procedure TForm.Form1Paint(Sender: TObject); begin

 // paint a line
 Canvas.Pen.Color := clRed;
 Canvas.Line(0, 0, Width, Height);


Create a custom control which draws itself

Creating a custom control has the advantage of structuring your code and you can reuse the control. This approach is very fast, but it can still generate flickering if you don't draw to a TBitmap first and then draw to the canvas. On this case there is no need to use the OnPaint event of the control.

Here is an example custom control:


 Classes, SysUtils, Controls, Graphics, LCLType;


 TMyDrawingControl = class(TCustomControl)
   procedure EraseBackground(DC: HDC); override;
   procedure Paint; override;


procedure TMyDrawingControl.EraseBackground(DC: HDC); begin

 // Uncomment this to enable default background erasing
 //inherited EraseBackground(DC);


procedure TMyDrawingControl.Paint; var

 x, y: Integer;
 Bitmap: TBitmap;


 Bitmap := TBitmap.Create;
   // Initializes the Bitmap Size
   Bitmap.Height := Height;
   Bitmap.Width := Width;

   // Draws the background
   Bitmap.Canvas.Pen.Color := clWhite;
   Bitmap.Canvas.Rectangle(0, 0, Width, Height);

   // Draws squares
   Bitmap.Canvas.Pen.Color := clBlack;
   for x := 1 to 8 do
     for y := 1 to 8 do
       Bitmap.Canvas.Rectangle(Round((x - 1) * Width / 8), Round((y - 1) * Height / 8),
         Round(x * Width / 8), Round(y * Height / 8));
   Canvas.Draw(0, 0, Bitmap);

 inherited Paint;


and how we create it on the form: <delphi>procedure TMyForm.FormCreate(Sender: TObject); begin

 MyDrawingControl := TMyDrawingControl.Create(Self);
 MyDrawingControl.Height := 400;
 MyDrawingControl.Width := 500;
 MyDrawingControl.Top := 0;
 MyDrawingControl.Left := 0;
 MyDrawingControl.Parent := Self;
 MyDrawingControl.DoubleBuffered := True;


It is destroyed automatically, because we use Self as owner.

Setting Top and Left to zero is not necessary, since this is the standard position, but is done so to reinforce where the control will be put.

"MyDrawingControl.Parent := Self;" is very important and you won't see your control if you don't do so.

"MyDrawingControl.DoubleBuffered := True;" is required to avoid flickering on Windows. It has no effect on gtk.

Using A.J. Venter's gamepack

The gamepack approach is to draw everything to one double-buffered canvas, which only gets updated to the visible canvas when you are ready. This takes quite a bit of code, but it has the advantage of being able to do large rapidly changing scenes with multiple sprites on them. If you wish to use this approach, you may be interested in A.J. Venter's gamepack, a set of components for game development in Lazarus, which provides a double-buffered display area component as well as a sprite component, designed to integrate well with one another.

Download: A.J. Venter's GamePack 1.0

Outdated links:

Image formats

Here is a table with the adequate class to use for each image format.

Format Image class Unit
Cursor (cur) TCursor Graphics
Bitmap (bmp) TBitmap Graphics
Windows icon (ico) TIcon Graphics
Mac OS X icon (icns) TicnsIcon Graphics
Pixmap (xpm) TPixmap Graphics
Portable Network Graphic (png) TPortableNetworkGraphic Graphics
JPEG (jpg, jpeg) TJpegImage Graphics
PNM (pnm) TPortableAnyMapGraphic Graphics

See also the list of fcl-image supported formats.

Converting formats

Sometimes it must be necessary to convert one graphic type to another. One of the ways is to convert a graphic to intermediate format, and then convert it to TBitmap. Most of the formats can create an image from TBitmap.

Converting Bitmap to PNG and saving it to a file:

<delphi>procedure SaveToPng(const bmp: TBitmap; PngFileName: String); var

 png : TPortableNetworkGraphic; 


 png := TPortableNetworkGraphic.Create;


Pixel Formats


The internal pixel format for TColor in the LCL is the XXBBGGRR format, which matches the native Windows format and is opposite to most other libraries, which use AARRGGBB. The XX part is used to identify if the color is a fixed color, which case XX should be 00 or if it is an index to a system color. There is no space reserved for an alpha channel.

To convert from separate RGB channels to TColor use:

<delphi>RGBToColor(RedVal, GreenVal, BlueVal);</delphi>

To get each channel of a TColor variable use the Red, Green and Blue functions:

<delphi>RedVal := Red(MyColor); GreenVal := Green(MyColor); BlueVal := Blue(MyColor);</delphi>


TFPColor uses the AARRGGBB format common to most libraries, but it uses 16-bits for the depth of each color channel, totaling 64-bits per pixel, which is unusual. This does not necessarily mean that images will consume that much memory, however. Images created using TRawImage+TLazIntfImage can have any internal storage format and then on drawing operations TFPColor is converted to this internal format.

The unit Graphics provides routines to convert between TColor and TFPColor:

<delphi> function FPColorToTColorRef(const FPColor: TFPColor): TColorRef; function FPColorToTColor(const FPColor: TFPColor): TColor; function TColorToFPColor(const c: TColorRef): TFPColor; overload; function TColorToFPColor(const c: TColor): TFPColor; overload; // does not work on system color </delphi>

Drawing with fcl-image

You can draw images which won't be displayed in the screen without the LCL, by just using fcl-image directly. For example a program running on a webserver without X11 could benefit from not having a visual library as a dependency. FPImage (alias fcl-image) is a very generic image and drawing library written completely in pascal. In fact the LCL uses FPImage too for all the loading and saving from/to files and implements the drawing function through calls to the widgetset (winapi, gtk, carbon, ...). Fcl-image on the other hand also has drawing routines.

For more information, please read the article about fcl-image.