[ SOLVED ] Lazarus/FPC FillRect drives me nuts and crazy lazarus / 39-Graphics ---------------------------------------------------------------------- jipété · 2024-05-26 10:03 Hello, found in an old project, related to TImage : 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 TForm1.btnTestClick(Sender: TObject);begin  with Image1.Canvas do begin    Clear; // ! \\ // ! \\ MANDATORY if not, black display // ! \\     // http://forum.lazarus.freepascal.org/index.php?topic=12558.0    Brush.Style := bsSolid;    Brush.Color := clRed;    FillRect(0, 0, Width, Height); // ok    { Save to file }    image1.Picture.SaveToFile ('test+4params_DrawTest.png'); // ok  end;end; That's ok, but 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 ...    Brush.Color := clRed;    FillRect(image1.BoundsRect); // other way, see https://forum.lazarus.freepascal.org/index.php/topic,12558.msg6466 2.html#msg64662    { Save to file }    image1.Picture.SaveToFile ('test+BoundsRect_DrawTest.png'); // NOT ok !  end;end; For BoundsRect, see https://forum.lazarus.freepascal.org/index.php/top ic,12558.msg64659.html#msg64659. I really don't understand... Have a nice day, ---------------------------------------------------------------------- jamie · 2024-05-26 13:41 BoundsRect reports location of image within the parent control. If you look at the LEFT, TOP of Timage you should see it match the BoundsRect you are getting minus some special border operations. If the image isn't fully left,top of the parent control, it will report an offset. also, beware that using the Width,Height of the canvas isn't reliable because it can grow larger than what your drawing surface is. ---------------------------------------------------------------------- KodeZwerg · 2024-05-26 13:50 From what I see two mini lessons, never use TImage to actually draw something and second always use a image format that does do what you want to do and when you finished painting assign it to display control, like TImage. At least my ruleset is like that. TImage for display, TSomeImage for drawing (TBitmap exemplary) ---------------------------------------------------------------------- KodeZwerg · 2024-05-26 14:27 Here is your doing in a (for my knowledge) corrected way: 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 TForm1.Button1Click(Sender: TObject);  procedure FillTImage(const AImage: TImage; const AColor: TColor);  var    LBitmap: TBitmap;  begin    LBitmap := TBitmap.Create;    try      // to simplify, use controls size      LBitmap.SetSize(AImage.Width, AImage.Height);      LBitmap.Canvas.Brush.Style := bsSolid;      LBitmap.Canvas.Brush.Color := AColor;      LBitmap.Canvas.FillRect(LBitmap.Canvas.ClipRect);    AImage.Picture.Assign(LBitmap);    finally      LBitmap.Free;    end;  end;  function SaveTImageAsPNG(const AImage: TImage; const AFilename: string): Boolean;  var    png: TPortableNetworkGraphic;  begin    Result := False;    // not overwrite files    if FileExists(AFilename) then      Exit;    png := TPortableNetworkGraphic.Create;    try      png.Assign(AImage.Picture.Graphic);      png.SaveToFile(AFilename);  finally      png.Free;    end;    Result := FileExists(AFilename);  end;var  R, G, B: Byte;begin  Randomize;  R := Random(High(Byte));  G := Random(High(Byte));  B := Random(High(Byte));  FillTImage(Image1, RGBToColor(R, G, B));  SaveTImageAsPNG(Image1, 'DrawTest.png');end;What this code does: It create and fills a TBitmap with some random color, including its assignment to TImage class Saves whatever content the TImage got as a PNG file. I've nested the methods, feel free to re-use elsewhere. ---------------------------------------------------------------------- jipété · 2024-05-26 16:31 Quote from: jamie on May 26, 2024, 03:41:41 pmBoundsRect reports location of image within the parent control.Understood, I'll remove BoundsRect from my mind, thank you. Quote from: KodeZwerg on May 26, 2024, 03:50:51 pmnever use TImage... So, why not use image1.Picture, like in that example ? : 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 Image1.Picture.Bitmap.LoadFromFile(Application.Location + 'img_sources/cielab_4ico.bmp');A oneliner which works fine, and easy to remember, for me it's the best ! Thanks for your code, I'll use it for trying to remove TBitmap off my life and use FPImage : too many problems with TBitmap and PixelFormat, for example. Quote from: KodeZwerg on May 26, 2024, 03:50:51 pm... to actually draw something And I don't draw, I just ask the system to do that, again, with oneliners. Quote from: KodeZwerg on May 26, 2024, 04:27:02 pmWhat this code does: It create and fills a TBitmap with some random color, including its assignment to TImage class Saves whatever content the TImage got as a PNG file. Works fine, thanks a lot !  +++ Sorry, I cannot add "[ SOLVED ]" to my other topic, the Broken Example, due to limitations from forum,.. ---------------------------------------------------------------------- wp · 2024-05-26 16:49 Quote from: jipété on May 26, 2024, 06:31:31 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Image1.Picture.Bitmap.LoadFromFile(Application.Location + 'img_sources/cielab_4ico.bmp');Better to remember the following code which is much more universal since it works with every registered image format while your code works only with the bmp format: 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 Image1.Picture.LoadFromFile(....); Quote from: jipété on May 26, 2024, 06:31:31 pmtrying to remove TBitmap off my life and use FPImage Although I am an absolute friend of fcl-image this is not a good idea. Many things are much easier to achieve with TBitmap than with FPImage. And the associated fcl-image library (and to a lesser extent the IntfGraphics library with TLazIntfImage and TLazCanvas) are not complete: the Pen.Width in the Ellipse or Polygon methods can only be 1, there is no GradientFill, poor region support, and drawing text is quite complicated. No - do not forget TBitmap. Like most things in life, everything has advantages and disadvantages... ---------------------------------------------------------------------- KodeZwerg · 2024-05-26 16:52 Quote from: jipété on May 26, 2024, 06:31:31 pmQuote from: KodeZwerg on May 26, 2024, 03:50:51 pmnever use TImage... So, why not use image1.Picture, like in that example ? : 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 Image1.Picture.Bitmap.LoadFromFile(Application.Location + 'img_sources/cielab_4ico.bmp');A oneliner which works fine, and easy to remember, for me it's the best ! For such purposes the display class TImage is invented for, you did misunderstood my text. 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 with Image1.Canvas do begin....I do not say that "with" is worse... damn I said it :-/ You try to manipulate the canvas of the TImage container while "Image1.Picture.Bitmap.LoadFromFile" does load and assign a bitmap file (exact same way as I do by code). So direct access one graphics format for loading is cool or to simplify "Image1.Picture.LoadFromFile" should be enough to load and display any supported format. Accessing pixels of TImage.Canvas = just dont. I hope you understand my meaning and I agree, oneliners are cool but the context where those can be called must fit.