copying a jpeg image of a chart to the clipboard lazarus / 55-TAChart ---------------------------------------------------------------------- mtanner · 2020-12-18 10:54 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 · 2020-12-18 11:27 Borrowed and adapted from TChart.CopyToClipboardBitmap: 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);begin  with Chart1.SaveToImage(TJpegImage) do    try      SaveToClipboardFormat(RegisterClipboardFormat(MimeType));    finally    Free;    end;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][+][-]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);begin  Chart1.CopyToClipboard(TJpegImage);end; ---------------------------------------------------------------------- mtanner · 2020-12-18 11:38 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 · 2020-12-18 11:57 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 · 2020-12-18 12:05 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 · 2020-12-18 12:32 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][+][-]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);var  jpg: TRasterImage;begin  jpg := Chart1.SaveToImage(TJpegImage);  try    jpg.SaveToClipboardFormat(RegisterClipboarFormat(jpg.MimeType));  finally    jpg.Free;  end;end; ---------------------------------------------------------------------- mtanner · 2020-12-18 12:50 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 · 2020-12-18 13:07 What are your Laz/FP versions and operating system so that I can test with correct machine? ---------------------------------------------------------------------- mtanner · 2020-12-21 11:01 Using Lazarus version 2.0.10, FPC version 3.2.0, SVN revision 63526, on Win 7 64 bit. ---------------------------------------------------------------------- mtanner · 2020-12-21 11:02 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 · 2020-12-21 11:41 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  clipbrd; var  CF_JPEG: Integer = -1; procedure TForm1.Button1Click(Sender: TObject);var  bmp: TBitmap;  jpeg: TJpegImage;begin  bmp := GetFormImage;  try    jpeg := TJpegImage.Create;    try      jpeg.Assign(bmp);      if CF_JPEG = -1 then        CF_JPEG := RegisterClipboardFormat('image/jpeg');      jpeg.SaveToClipboardFormat(CF_JPEG);    finally      jpeg.Free;    end;  finally    bmp.Free;  end;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 · 2020-12-21 11:55 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.SaveImageTo(const AFilename: String);var  ABitmap: TBitmap;begin  ABitmap := TBitmap.Create;  try    ABitmap.SetSize(Width, Height);    PaintTo(ABitmap.Canvas, 0, 0);    ABitmap.SaveToFile(AFilename);    {or CopyToClipboard or whatever}  finally    ABitmap.Free;  end;end; Though, IIRC, it has the same drawback: it paints only the client area, not the non-client one drawn by the system. ---------------------------------------------------------------------- wp · 2020-12-21 12:03 This is (almost) what Form.GetFormImage is doing: 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 function TCustomForm.GetFormImage: TBitmap;var  ARect: TRect;begin  Result := TBitmap.Create;  try    Result.SetSize(ClientWidth, ClientHeight);    LCLIntf.GetWindowRect(Handle, ARect);    with GetClientOrigin do      PaintTo(Result.Canvas, ARect.Left - X, ARect.Top - Y);  except    Result.Free;    raise;  end;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.