Recent

Author Topic: copying a jpeg image of a chart to the clipboard  (Read 2067 times)

mtanner

  • Sr. Member
  • ****
  • Posts: 287
copying a jpeg image of a chart to the clipboard
« on: December 18, 2020, 11:54:28 am »
I have found some posts about saving a jpeg image of a chart to a file, but I cannot figure out how to copy a chart image to the clipboard. How do you do that?

wp

  • Hero Member
  • *****
  • Posts: 11910
Re: copying a jpeg image of a chart to the clipboard
« Reply #1 on: December 18, 2020, 12:27:24 pm »
Borrowed and adapted from TChart.CopyToClipboardBitmap:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   with Chart1.SaveToImage(TJpegImage) do
  4.     try
  5.       SaveToClipboardFormat(RegisterClipboardFormat(MimeType));
  6.     finally
  7.       Free;
  8.     end;
  9. end;

But I just added a new method TChart.CopyToClipboard(AClass: TRasterImageClass) which gets the desired image type as a parameter. So, if you are using trunk you can also call
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Chart1.CopyToClipboard(TJpegImage);
  4. end;

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: copying a jpeg image of a chart to the clipboard
« Reply #2 on: December 18, 2020, 12:38:04 pm »
Thanks for that. I'm using FPC 3.2 on Win7. I found RegisterClipboardFormat in the unit jwawinuser, but than I get a compiler error of thre MimeType parameter, sayinh "got ansistring expected pchar".  Not sure how to proceed?

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: copying a jpeg image of a chart to the clipboard
« Reply #3 on: December 18, 2020, 12:57:17 pm »
My basic probem is that I don't know what a"mimetype" is. Ob googling I find referebces to getting a mimetype from a file extension, but that doesn't tell me what a mimetype is. I imaging it's something that says jpeg or text or whatever, but I see no examples of actual instances of a mimetype.

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: copying a jpeg image of a chart to the clipboard
« Reply #4 on: December 18, 2020, 01:05:19 pm »
I have tried varous things fro mimetype, most likely seems to be "image/jpeg", but although the code compiles and runs, there seems to be nothing on the clipboard afterwards.

wp

  • Hero Member
  • *****
  • Posts: 11910
Re: copying a jpeg image of a chart to the clipboard
« Reply #5 on: December 18, 2020, 01:32:48 pm »
Bad "with" instruction...

Chart.SaveToImage(TJpegImage) creates a TRasterImage instance of the specified type and draws the chart on it. Because of the "with" the MimeType is not meant to be a separate variable but a property of the TRasterImage. And because the TJpegImage knows how to handle this there is nothing to do for you.

Here is the code without the "with", and I hope you understand:
Code: Pascal  [Select][+][-]
  1.    
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. var
  4.   jpg: TRasterImage;
  5. begin
  6.   jpg := Chart1.SaveToImage(TJpegImage);
  7.   try
  8.     jpg.SaveToClipboardFormat(RegisterClipboarFormat(jpg.MimeType));
  9.   finally
  10.     jpg.Free;
  11.   end;
  12. end;

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: copying a jpeg image of a chart to the clipboard
« Reply #6 on: December 18, 2020, 01:50:29 pm »
Thanks, just trried that, but still gte compiler error "got ansistring expected pchar" of jpg.mimetype. If I put "image/jpeg" uin explicitly thebn it runs but nothing on the clipboard. Maybe its's something to be with FPC bersion of being on Win7.

wp

  • Hero Member
  • *****
  • Posts: 11910
Re: copying a jpeg image of a chart to the clipboard
« Reply #7 on: December 18, 2020, 02:07:21 pm »
What are your Laz/FP versions and operating system so that I can test with correct machine?

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: copying a jpeg image of a chart to the clipboard
« Reply #8 on: December 21, 2020, 12:01:25 pm »
Using Lazarus version 2.0.10, FPC version 3.2.0, SVN revision 63526, on Win 7 64 bit.

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: copying a jpeg image of a chart to the clipboard
« Reply #9 on: December 21, 2020, 12:02:50 pm »
Just to extend the question a bit, is it also possible to capture a jpeg of a form, rather than just the chart? ( Mainly I want to do charts, but have an occcasional wish to capture a form).

wp

  • Hero Member
  • *****
  • Posts: 11910
Re: copying a jpeg image of a chart to the clipboard
« Reply #10 on: December 21, 2020, 12:41:03 pm »
The form has a method GetFormImage which renders the form into a TBitmap. If you need a jpeg you must convert the bitmap to jpeg before copying to the clipboard:
Code: Pascal  [Select][+][-]
  1. uses
  2.   clipbrd;
  3.  
  4. var
  5.   CF_JPEG: Integer = -1;
  6.  
  7. procedure TForm1.Button1Click(Sender: TObject);
  8. var
  9.   bmp: TBitmap;
  10.   jpeg: TJpegImage;
  11. begin
  12.   bmp := GetFormImage;
  13.   try
  14.     jpeg := TJpegImage.Create;
  15.     try
  16.       jpeg.Assign(bmp);
  17.       if CF_JPEG = -1 then
  18.         CF_JPEG := RegisterClipboardFormat('image/jpeg');
  19.       jpeg.SaveToClipboardFormat(CF_JPEG);
  20.     finally
  21.       jpeg.Free;
  22.     end;
  23.   finally
  24.     bmp.Free;
  25.   end;
  26. end;  

I tested this on Win 10, Laz 2.0.10/FPC 3.2.0 / 64 bit, and it works (there was an error initially because I had forgotten to add "clipbrd" to "uses").

GetFormImage renders the form WITHOUT the non-client areas such as title bar and border. I don't know how this can be handled (except for taking a screenshot of the entire screen and cropping to form size). If everything breaks you can still press ALT+PRINT which copies the current form to the clipboard, but as a TBitmap only.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: copying a jpeg image of a chart to the clipboard
« Reply #11 on: December 21, 2020, 12:55:54 pm »
Alternatively, though a little more convoluted, you can use the form's PaintTo() method to paint it to any canvas like, for example, a TBitmap's:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SaveImageTo(const AFilename: String);
  2. var
  3.   ABitmap: TBitmap;
  4. begin
  5.   ABitmap := TBitmap.Create;
  6.   try
  7.     ABitmap.SetSize(Width, Height);
  8.     PaintTo(ABitmap.Canvas, 0, 0);
  9.     ABitmap.SaveToFile(AFilename);
  10.     {or CopyToClipboard or whatever}
  11.   finally
  12.     ABitmap.Free;
  13.   end;
  14. end;

Though, IIRC, it has the same drawback: it paints only the client area, not the non-client one drawn by the system.
« Last Edit: December 21, 2020, 01:17:51 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 11910
Re: copying a jpeg image of a chart to the clipboard
« Reply #12 on: December 21, 2020, 01:03:01 pm »
This is (almost) what Form.GetFormImage is doing:
Code: Pascal  [Select][+][-]
  1. function TCustomForm.GetFormImage: TBitmap;
  2. var
  3.   ARect: TRect;
  4. begin
  5.   Result := TBitmap.Create;
  6.   try
  7.     Result.SetSize(ClientWidth, ClientHeight);
  8.     LCLIntf.GetWindowRect(Handle, ARect);
  9.     with GetClientOrigin do
  10.       PaintTo(Result.Canvas, ARect.Left - X, ARect.Top - Y);
  11.   except
  12.     Result.Free;
  13.     raise;
  14.   end;
  15. end;  
Except for the image size and origin so that part of title bar and border is include. Combining them and some calculation for the window border size may solve it.
« Last Edit: December 21, 2020, 01:14:49 pm by wp »

 

TinyPortal © 2005-2018