Recent

Author Topic: Copy stringgrid canvas in image  (Read 2796 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 1904
    • Lazarus and Free Pascal italian community
Copy stringgrid canvas in image
« on: July 13, 2018, 11:41:07 am »
For my project i have necessity to copy my string grid (with image and rectangol color) into image. But into Mac Os X not work return all black image. Where is the problem?

Thank you

PS: sGanttChart1 is my TStringGrid

Code: Pascal  [Select]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var
  3.    Bitmap : TBitmap;
  4.    DC     : HDC ;
  5. begin
  6.  
  7.      Bitmap := TBitmap.Create;
  8.      DC := GetDC(Self.sGanttChart1.Handle);
  9.      Bitmap.LoadFromDevice(DC);
  10.      Bitmap.SaveToFile('/tmp/screen.png');
  11.      ReleaseDC(Self.sGanttChart1.Handle, DC) ;
  12.      Bitmap.Free;
  13.      ShowMessage('ok');
  14. end;
  15.  
  16. procedure TForm1.Button4Click(Sender: TObject);
  17. var
  18.   h: THandle;
  19.   bmp: TBitmap;
  20. begin
  21.   h := sGanttChart1.Handle;
  22.   bmp := TBitmap.Create;
  23.   bmp.Width := sGanttChart1.Width;
  24.   bmp.Height := sGanttChart1.Height;
  25.   Image1.Width := sGanttChart1.Width;
  26.   Image1.Height := sGanttChart1.Height;
  27.   BitBlt(bmp.Canvas.Handle, 0, 0, sGanttChart1.Width, sGanttChart1.Height, sGanttChart1.Canvas.handle, 0, 0, SRCCOPY);
  28.   Image1.Picture.Bitmap.Assign(bmp);
  29.   //Image1.Picture.Bitmap.SaveToFile('c:\sg1.bmp');
  30.   bmp.Free;
  31.  
  32. end;        
  33.  
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

RayoGlauco

  • Jr. Member
  • **
  • Posts: 85
  • Beers: 1567
Re: Copy stringgrid canvas in image
« Reply #1 on: July 13, 2018, 01:16:35 pm »
This code works for me (tested on Windows):

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   bmp: Graphics.TBitmap;
  4. begin
  5.   bmp := Graphics.TBitmap.Create;
  6.   bmp.SetSize(StringGrid1.ClientWidth, StringGrid1.ClientHeight);
  7.   bmp.Canvas.Brush.Color := clWhite;
  8.   bmp.Canvas.FillRect(0, 0, bmp.Width, bmp.Height);
  9.   StringGrid1.PaintTo(bmp.Canvas, 0, 0);
  10.   bmp.SaveToFile('test.bmp');
  11.   FreeAndNil(bmp);
  12. end;  
To err is human, but to really mess things up, you need a computer.

xinyiman

  • Hero Member
  • *****
  • Posts: 1904
    • Lazarus and Free Pascal italian community
Re: Copy stringgrid canvas in image
« Reply #2 on: July 13, 2018, 01:43:25 pm »
This code works for me (tested on Windows):

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   bmp: Graphics.TBitmap;
  4. begin
  5.   bmp := Graphics.TBitmap.Create;
  6.   bmp.SetSize(StringGrid1.ClientWidth, StringGrid1.ClientHeight);
  7.   bmp.Canvas.Brush.Color := clWhite;
  8.   bmp.Canvas.FillRect(0, 0, bmp.Width, bmp.Height);
  9.   StringGrid1.PaintTo(bmp.Canvas, 0, 0);
  10.   bmp.SaveToFile('test.bmp');
  11.   FreeAndNil(bmp);
  12. end;  

Thank you, but not work on Mac. Return all white image
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

josh

  • Hero Member
  • *****
  • Posts: 747
Re: Copy stringgrid canvas in image
« Reply #3 on: July 13, 2018, 02:14:45 pm »
No tested on Mac..

Maybe?

Code: Pascal  [Select]
  1. var bmp:tbitmap;
  2. begin
  3.   bmp:=TBitmap.Create;
  4.   bmp.SetSize(stringgrid1.ClientWidth,stringgrid1.ClientHeight);
  5.   bmp.PixelFormat:= pf24bit;
  6.   bmp.Canvas.Brush.Color := clWhite;
  7.   bmp.clear;
  8.   StringGrid1.PaintTo(bmp.Canvas, 0, 0);
  9.   bmp.SaveToFile('test.bmp');
  10.   bmp.free;
  11. end;  
  12.  
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

xinyiman

  • Hero Member
  • *****
  • Posts: 1904
    • Lazarus and Free Pascal italian community
Re: Copy stringgrid canvas in image
« Reply #4 on: July 13, 2018, 02:21:58 pm »
No tested on Mac..

Maybe?

Code: Pascal  [Select]
  1. var bmp:tbitmap;
  2. begin
  3.   bmp:=TBitmap.Create;
  4.   bmp.SetSize(stringgrid1.ClientWidth,stringgrid1.ClientHeight);
  5.   bmp.PixelFormat:= pf24bit;
  6.   bmp.Canvas.Brush.Color := clWhite;
  7.   bmp.clear;
  8.   StringGrid1.PaintTo(bmp.Canvas, 0, 0);
  9.   bmp.SaveToFile('test.bmp');
  10.   bmp.free;
  11. end;  
  12.  

Not work, return all black image  %) %)
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

josh

  • Hero Member
  • *****
  • Posts: 747
Re: Copy stringgrid canvas in image
« Reply #5 on: July 13, 2018, 02:53:44 pm »
What about changing the paintto to :=
Code: Pascal  [Select]
  1. //StringGrid1.PaintTo(bmp.Canvas, 0, 0); // Removed for testing purposes..
  2. bmp.Canvas.CopyRect(rect(0, 0, bmp.Width, bmp.Height),stringgrid1.canvas,rect(0, 0, stringgrid1.ClientWidth, stringgrid1.ClientHeight));
  3.  
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

xinyiman

  • Hero Member
  • *****
  • Posts: 1904
    • Lazarus and Free Pascal italian community
Re: Copy stringgrid canvas in image
« Reply #6 on: July 13, 2018, 02:59:51 pm »
Not work, return all black image
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: Copy stringgrid canvas in image
« Reply #7 on: July 13, 2018, 03:03:07 pm »
Not work, return all black image

Test with Cocoa widgetset if you're not already doing that. If bug there too, file bug report. If no bug, use Cocoa widgetset.

MISV

  • Hero Member
  • *****
  • Posts: 603
Re: Copy stringgrid canvas in image
« Reply #8 on: July 13, 2018, 03:07:53 pm »
Cocoa is the future - carbon apps will soon no run on Mac. As Phil said, if the bug is there on Cocoa as well report bug! :)

Hint: I use fpcupdeluxe to get newest trunk version of Cocoa (easiest way to get started I think)

xinyiman

  • Hero Member
  • *****
  • Posts: 1904
    • Lazarus and Free Pascal italian community
Re: Copy stringgrid canvas in image
« Reply #9 on: July 13, 2018, 03:34:08 pm »
Cocoa is the future - carbon apps will soon no run on Mac. As Phil said, if the bug is there on Cocoa as well report bug! :)

Hint: I use fpcupdeluxe to get newest trunk version of Cocoa (easiest way to get started I think)

Problem with my actual version of cocoa lazarus 1.8.4
I test install cocoa lazarus trunk yesterday but not function. When i start ide return arithmetic overflow.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: Copy stringgrid canvas in image
« Reply #10 on: July 13, 2018, 03:42:35 pm »
Cocoa is the future - carbon apps will soon no run on Mac. As Phil said, if the bug is there on Cocoa as well report bug! :)

Hint: I use fpcupdeluxe to get newest trunk version of Cocoa (easiest way to get started I think)

Problem with my actual version of cocoa lazarus 1.8.4
I test install cocoa lazarus trunk yesterday but not function. When i start ide return arithmetic overflow.

You can compile your app with Cocoa using lazbuild regardless of whether the Cocoa IDE is available.

https://macpgmr.github.io/MacXPlatform/UsingCocoaFromTrunk.html

josh

  • Hero Member
  • *****
  • Posts: 747
Re: Copy stringgrid canvas in image
« Reply #11 on: July 13, 2018, 03:45:58 pm »
Hi

If you click the OK Button, and the IDE stays there; try rebuilding the IDE from the Tools Menu, this has sometimes fixed an odd Startup with Laz-FPC-Trunk->for Cocoa.

Please always check that you are using the very latest Trunk version when testing Laz(1.9.0) Cocoa, as it is being rapidly developed multi comit changes per day in some cases; and it is probable that a fix/addition may have a knock on effect, if so report to forum to check if others can confirm and then post on bugtracker, this is the best route to getting Lazarus-Cocoa working...
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

xinyiman

  • Hero Member
  • *****
  • Posts: 1904
    • Lazarus and Free Pascal italian community
Re: Copy stringgrid canvas in image
« Reply #12 on: July 13, 2018, 03:59:26 pm »
Hi

If you click the OK Button, and the IDE stays there; try rebuilding the IDE from the Tools Menu, this has sometimes fixed an odd Startup with Laz-FPC-Trunk->for Cocoa.

Please always check that you are using the very latest Trunk version when testing Laz(1.9.0) Cocoa, as it is being rapidly developed multi comit changes per day in some cases; and it is probable that a fix/addition may have a knock on effect, if so report to forum to check if others can confirm and then post on bugtracker, this is the best route to getting Lazarus-Cocoa working...

Yes, i test of rebuild with normal ide but not function. Return error with ok and abort button. I reinstall all next week with hope
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

MISV

  • Hero Member
  • *****
  • Posts: 603
Re: Copy stringgrid canvas in image
« Reply #13 on: July 13, 2018, 07:50:35 pm »
Hi

If you click the OK Button, and the IDE stays there; try rebuilding the IDE from the Tools Menu, this has sometimes fixed an odd Startup with Laz-FPC-Trunk->for Cocoa.

Please always check that you are using the very latest Trunk version when testing Laz(1.9.0) Cocoa, as it is being rapidly developed multi comit changes per day in some cases; and it is probable that a fix/addition may have a knock on effect, if so report to forum to check if others can confirm and then post on bugtracker, this is the best route to getting Lazarus-Cocoa working...

Sounds like OP has some problem as I have I have reported here:
https://bugs.freepascal.org/view.php?id=33969

(startup errors + build ide not possible)