Show: Delphi C++
Display Preferences

Drawing On the Bitmap

From RAD Studio
Jump to: navigation, search

Go Up to Adding an Image Control


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 Vcl.Graphics.TBitmap.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.

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: \ mygraphic.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;
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

Personal tools
RAD Studio 10.2 Tokyo
In other languages
Previous Versions
Assistance