[SOLVED] Why setting TBitmap's PixelFormat property resets the Canvas Brush/Pen? lazarus / 39-Graphics ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-09-19 06:22 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  Bitmap: TBitmap;begin  Bitmap:= TBitmap.Create;  Bitmap.Width:= cWidth;  Bitmap.Height:= cHeight;  Bitmap.Canvas.Brush.Color:= clWhite;  Bitmap.PixelFormat:= pf24bit;  Bitmap.Canvas.FillRect(0,0,cWidth,cHeight);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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  Bitmap: TBitmap;begin  Bitmap:= TBitmap.Create;  Bitmap.PixelFormat:= pf24bit;  Bitmap.Width:= cWidth;  Bitmap.Height:= cHeight;  Bitmap.Canvas.Brush.Color:= clWhite;  Bitmap.Canvas.FillRect(0,0,cWidth,cHeight);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 ---------------------------------------------------------------------- speter · 2021-09-19 23:41 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. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-09-19 23:46 Hey speter, Quote from: speter on September 20, 2021, 01:41:28 amWhen 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 ---------------------------------------------------------------------- flowCRANE · 2021-09-20 11:39 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCustomBitmap.SetPixelFormat(AValue: TPixelFormat);begin  if AValue = FPixelFormat then Exit;  {$IFDEF VerboseLCLTodos}{$note todo copy image into new format }{$ENDIF}  FreeImage;  FPixelFormat := AValue;end; When the format is changed, the image is freed, so previous settings are no longer valid. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-09-20 13:05 Hey Furious Programming, Quote from: furious programming on September 20, 2021, 01:39:50 pmTo 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