Recent

Author Topic: Tlvlgraphcontrol save on bitmap  (Read 2412 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Tlvlgraphcontrol save on bitmap
« on: January 10, 2018, 04:08:46 pm »
Hi guys, how to title how do I save the contents of a tlvlgraphcontrol in a bitmap file?

Thank you
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Tlvlgraphcontrol save on bitmap
« Reply #1 on: February 01, 2018, 11:05:13 am »
no suggestion?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

m.abudrais

  • Jr. Member
  • **
  • Posts: 54
Re: Tlvlgraphcontrol save on bitmap
« Reply #2 on: April 29, 2024, 12:23:00 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   C: TCanvas;
  4.   PNG: TPortableNetworkGraphic;
  5. begin
  6.   C := LvlGraphControl1.Canvas;
  7.   PNG := TPortableNetworkGraphic.Create;
  8.   PNG.SetSize(LvlGraphControl1.Width, LvlGraphControl1.Height);
  9.   PNG.Canvas.CopyRect(Rect(0, 0, LvlGraphControl1.Width, LvlGraphControl1.Height),
  10.     C, Rect(0, 0, LvlGraphControl1.Width, LvlGraphControl1.Height));
  11.   PNG.SaveToFile('ae.png');
  12. end;  
  13.  

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Tlvlgraphcontrol save on bitmap
« Reply #3 on: April 29, 2024, 01:04:16 am »
I would write it more in a generic way and free the PNG but after 6 years I do assume that the OP don't need it anymore :D
Code: Pascal  [Select][+][-]
  1. function SaveCanvasAsPNG(const ACanvas: TCanvas; const AFilename: string): Boolean;
  2. var
  3.   PNG: TPortableNetworkGraphic;
  4. begin
  5.   Result := False;
  6.   PNG := TPortableNetworkGraphic.Create;
  7.   try
  8.     PNG.SetSize(ACanvas.ClipRect.Width, ACanvas.ClipRect.Height);
  9.     PNG.Canvas.CopyMode := cmSrcCopy;
  10.     PNG.Canvas.CopyRect(Rect(0, 0, PNG.Width, PNG.Height),
  11.       ACanvas, Rect(0, 0, ACanvas.ClipRect.Width, ACanvas.ClipRect.Height));
  12.     PNG.SaveToFile(AFilename);
  13.     Result := True;
  14.   finally
  15.     PNG.Free;
  16.   end;
  17. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

m.abudrais

  • Jr. Member
  • **
  • Posts: 54
Re: Tlvlgraphcontrol save on bitmap
« Reply #4 on: April 29, 2024, 02:00:44 am »
Quote
OP don't need it anymore :D
probably  :), but it may help other people.
I was searching for the answer because my code was not working, the problem was Canvas.Width and canvas.Height always return 0 !! . this is my old code: 
Code: Pascal  [Select][+][-]
  1.     procedure TForm1.Button1Click(Sender: TObject);
  2.     var
  3.       C: TCanvas;
  4.       PNG: TPortableNetworkGraphic;
  5.     begin
  6.       C := LvlGraphControl1.Canvas;
  7.       PNG := TPortableNetworkGraphic.Create;
  8.       PNG.SetSize(C.Width, C.Height);
  9.       PNG.Canvas.CopyRect(Rect(0, 0, C.Width, C.Height),
  10.         C, Rect(0, 0, C.Width, C.Height));
  11.       PNG.SaveToFile('ae.png');
  12.     end;  
  13.      
Thanks.

wp

  • Hero Member
  • *****
  • Posts: 12069
Re: Tlvlgraphcontrol save on bitmap
« Reply #5 on: April 29, 2024, 01:07:58 pm »
Another way: Every TControl has a method PaintTo for painting on some other Canvas.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SaveToFile(const AFileName: String);
  2. var
  3.   png: TPortableNetworkGraphic;
  4. begin
  5.   png := TPortableNetworkGraphic.Create;
  6.   try
  7.     png.SetSize(LvlGraphControl1.Width, lvlGraphControl1.Height);
  8.     LvlGraphControl1.PaintTo(png.Canvas, 0, 0);
  9.     png.SaveToFile(AFileName);
  10.   finally
  11.     png.Free;
  12.   end;
  13. end;

 

TinyPortal © 2005-2018