Recent

Author Topic: [SOLVED] Why setting TBitmap's PixelFormat property resets the Canvas Brush/Pen?  (Read 840 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 757
  • Professional amateur ;-P
Hey Y'all,

Still asking for the lazCAPTCHA thingamaboob I made :)

While trying to set the background to white I got a solution on this thread but the solution kinda spawn another issue.

I'm now baffled by why setting the PixelFormat property of a TBitmap resets the Canvas Pen and/or Brush!!

Let me explain it with code.

If I do this:
Code: Pascal  [Select][+][-]
  1. var
  2.   Bitmap: TBitmap;
  3. begin
  4.   Bitmap:= TBitmap.Create;
  5.   Bitmap.Width:= cWidth;
  6.   Bitmap.Height:= cHeight;
  7.   Bitmap.Canvas.Brush.Color:= clWhite;
  8.   Bitmap.PixelFormat:= pf24bit;
  9.   Bitmap.Canvas.FillRect(0,0,cWidth,cHeight);
  10. end;
Then the FillRect is ineffective at painting a white rectangle. I'm presuming that setting the PixelFormat resets the Brush, but I'm not sure and that's why i need a clarification.

BUT if I do this:
Code: Pascal  [Select][+][-]
  1. var
  2.   Bitmap: TBitmap;
  3. begin
  4.   Bitmap:= TBitmap.Create;
  5.   Bitmap.PixelFormat:= pf24bit;
  6.   Bitmap.Width:= cWidth;
  7.   Bitmap.Height:= cHeight;
  8.   Bitmap.Canvas.Brush.Color:= clWhite;
  9.   Bitmap.Canvas.FillRect(0,0,cWidth,cHeight);
  10. end;
Then the FillRect does indeed draw a white rectangle.

Is this a bug that needs to be reported to the issue tracking on GitLab?

Many thanks in advance!!

Cheers,
Gus
« Last Edit: September 20, 2021, 03:05:48 pm by Gustavo 'Gus' Carreno »
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

speter

  • Full Member
  • ***
  • Posts: 223
Re: Why setting TBitmap's PixelFormat property resets the Canvas Brush/Pen?
« Reply #1 on: September 20, 2021, 01:41:28 am »
When the pixelformat is changed the current colours may no longer be valid (for example if it is set to 2bit then only black/white would be valid), so it makes sense for them to be reset.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 757
  • Professional amateur ;-P
Re: Why setting TBitmap's PixelFormat property resets the Canvas Brush/Pen?
« Reply #2 on: September 20, 2021, 01:46:29 am »
Hey speter,

When the pixelformat is changed the current colours may no longer be valid (for example if it is set to 2bit then only black/white would be valid), so it makes sense for them to be reset.

I was afraid that was the answer and it makes all the sense.

I'm just too of a newbie in terms of images that I wasn't sure.

Thank you SOOOO much for clarifying :)

Cheers,
Gus
« Last Edit: September 20, 2021, 03:00:49 pm by Gustavo 'Gus' Carreno »
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

furious programming

  • Hero Member
  • *****
  • Posts: 516
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Why setting TBitmap's PixelFormat property resets the Canvas Brush/Pen?
« Reply #3 on: September 20, 2021, 01:39:50 pm »
To keep the code fast and logical, you should always set the PixelFormat before setting the bitmap size and drawing. Look how the setter of this property look like:

Code: Pascal  [Select][+][-]
  1. procedure TCustomBitmap.SetPixelFormat(AValue: TPixelFormat);
  2. begin
  3.   if AValue = FPixelFormat then Exit;
  4.   {$IFDEF VerboseLCLTodos}{$note todo copy image into new format }{$ENDIF}
  5.   FreeImage;
  6.   FPixelFormat := AValue;
  7. end;

When the format is changed, the image is freed, so previous settings are no longer valid.
Lazarus 2.0.12 with FPC 3.2.0 (SVN Revision 64642), Windows 10 — all 64-bit

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 757
  • Professional amateur ;-P
Re: Why setting TBitmap's PixelFormat property resets the Canvas Brush/Pen?
« Reply #4 on: September 20, 2021, 03:05:20 pm »
Hey Furious Programming,

To keep the code fast and logical, you should always set the PixelFormat before setting the bitmap size and drawing.

First of all, thank you SO very much for that source deep dive, I appreciate the effort!!

And now it's clear why it fails.

I'm now sure that the change I made to have the PixelFormat set right after Bitmap:= TBitmap.Create; is the best practice.

Again, many thanks for the help, quite appreciated indeed!!

Cheers,
Gus
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018