RAD Studio |  |

Drawing On the Bitmap
See Also
Collapse All

To draw on a bitmap, use the image control's canvas and attach the mouse-event handlers to the appropriate events in the image control. Typically, you would use region operations (fills, rectangles, polylines, and so on). These are fast and efficient methods of drawing.

An efficient way to draw images when you need to access individual pixels is to use the bitmap ScanLine property. For general-purpose usage, you can set up the bitmap pixel format to 24 bits and then treat the pointer returned from ScanLine as an array of RGB. Otherwise, you will need to know the native format of the ScanLine property. This example shows how to use ScanLine to get pixels one line at a time.

Copy Code

    procedure TForm1.Button1Click(Sender: TObject);
    // This example shows drawing directly to the Bitmap
    var
    x,y : integer;
    Bitmap : TBitmap;
    P : PByteArray;
    begin
    Bitmap := TBitmap.create;
    try
    Bitmap.LoadFromFile("C:\Program Files\Borland\Delphi 4\Images\Splash\256color\factory.bmp");
    for y := 0 to Bitmap.height -1 do
    begin
    P := Bitmap.ScanLine[y];
    for x := 0 to Bitmap.width -1 do
    P[x] := y;
    end;
    canvas.draw(0,0,Bitmap);
    finally
    Bitmap.free;
    end;
    end;

Copy Code

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
    // This example shows drawing directly to the Bitmap
    Byte *ptr;
    try
    {
    pBitmap->LoadFromFile("C:\\Program Files\\Borland\\CBuilder\\Images\\Splash\\256color\\factory.bmp ");
    for (int y = 0; y < pBitmap->Height; y++)
    {
    ptr = pBitmap->ScanLine[y];
    for (int x = 0; x < pBitmap->Width; x++)
    ptr[x] = (Byte)y;
    }
    Canvas->Draw(0,0,pBitmap);
    }
    catch (...)
    {
    ShowMessage("Could not load or alter bitmap");
    }
    delete pBitmap;
    }

See Also

Placing the Control

Setting the Initial Bitmap Size

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!