Search code examples
delphibitmappixelscanline

Delphi TBitmap - why are Pixels and ScanLine different?


While using a 32 bit TBitmap, I switched from Canvas.Pixels to ScanLine.

I then set the value to Red, only to find it was displayed as blue.

Any idea why?

Here's a code excerpt:

procedure TForm1.FormPaint(Sender: TObject);
var
  varBitmap: TBitmap;
  pLock: PIntegerArray;
  iColor: integer;
begin
  varBitmap := TBitmap.Create;
  varBitmap.PixelFormat := pf32bit;
  varBitmap.Width := 800;
  varBitmap.Height := 600;

  // Set Pixels to Red
  varBitmap.Canvas.Pixels[0, 0] := $0000FF;

  // Shows $FF0000 (blue)
  pLock := varBitmap.ScanLine[0];
  iColor := pLock[0];
  ShowMessageFmt('%x', [iColor]);

  // Set ScanLine to Red
  pLock[0] := $0000FF;

  // Displays a blue pixel
  Canvas.Draw(0, 0, varBitmap);
end;

It seems that somehow TColor is not the same as what is in memory, but that makes no sense.

Any suggestions welcome. ;)


Solution

  • 32bit pixel data is in $AARRGGBB format. You are setting the Blue component, not the Red component. Use $FF0000 instead of $0000FF. Or better, use the RGB() function.