[Lazarus] Fast drawing to canvas

Bernd prof7bit at googlemail.com
Wed Feb 8 02:15:38 CET 2012


2012/2/8  <dhkblaszyk at zeelandnet.nl>:
> Given a list of bitmaps, what would be the fastest way to draw them to the
> screen consequtively?

I recently had a similar thing to do and I ended up simply writing the
data directly into the RawImage of the TImage.Bitmap. This is a code
snippet from my program which will copy the bitmap from a camera (the
image is in memory already as an array of record with red, green blue
bytes, I could copy the longword directly because they colors are
ordered differently):

var
    LinePointer, PixelPointer: PByte;
    Display: TImage;
    ....

begin
  with Display.Picture.Bitmap do begin
    Width := CamDimensions.Width;
    Height := CamDimensions.Height;
    Clear;
    with RawImage do begin;
      BytesPerLine := Description.BytesPerLine;
      BytesPerPixel := Description.BitsPerPixel div 8;
      RedShift := Description.RedShift;
      GreenShift := Description.GreenShift;
      BlueShift := Description.BlueShift;
    end;
  end;

    ....

    LinePointer := Display.Picture.Bitmap.RawImage.Data;
    LineOffset := 0;
    for y:=0 to CamDimensions.Height-1 do begin
      PixelPointer := LinePointer;
      for x:=0 to CamDimensions.Width-1 do begin
        with CamImage[LineOffset + x] do begin
          PInteger(PixelPointer)^ := Red << RedShift
                                  or Green << GreenShift
                                  or Blue << BlueShift;
          PixelPointer += BytesPerPixel;
        end;
      end;
      LineOffset += CamDimensions.Width;
      LinePointer += BytesPerLine;
    end;


And very important: before you call this method call BeginUpdate and
after you are done EndUpdate. The last one should trigger the paint
method. And don't do this from within the paint method, leave the
paint method alone!

This example will most likely not directly be suitable for your
bitmaps, they might not be organized the same way as my camera images
but its the same principle:

Image.BeginUpdate;
Image.Picture.Bitmap.Clear;
write the data directly to Image.Picture.Bitmap.RawImage.Data^
Image.EndUpdate

The last one will then trigger the painting on the screen and *don't*
use the OnPaint event yourself, leave this completely alone.




More information about the Lazarus mailing list