Problems with TClipboard lazarus / 31-LCL ---------------------------------------------------------------------- simone · 2017-12-21 16:07 Dear all, I'd like to allow copy/paste from graphs drawn with my application, based on EvsSimpleGraph package, towards externals applications. May be I'm missing something, but with the following demo code, after execution, if I paste on Windows Paint, I have the wrong result showed in the second screenshot... Does anyone have any suggestions? Thanks in advance. 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 unit Unit1;{$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Clipbrd, USimpleGraph; type  TForm1 = class(TForm)    Graph : TEvsSimpleGraph;    procedure FormShow(Sender: TObject);  private    procedure CopyToClipBoard(aGraph : TEvsSimpleGraph; SrcRect : TRect);  public    end; var  Form1: TForm1; implementation{$R *.lfm} procedure TForm1.FormShow(Sender: TObject);begin  Graph:=TEvsSimpleGraph.Create(Self);  Graph.Align:=alClient;  Graph.Parent:=Self;  Graph.InsertNode(Rect(50,50,100,100));  CopyToClipBoard(Graph,Rect(0,0,110,110));end; procedure TForm1.CopyToClipBoard(aGraph : TEvsSimpleGraph; SrcRect : TRect);var  BmpSrc,BmpDst : TBitmap;  DstRect : TRect;begin  BmpSrc:=TBitmap.Create;  BmpDst:=TBitmap.Create;  aGraph.CopyToGraphic(BmpSrc);  DstRect:=Rect(0,0,SrcRect.Width,SrcRect.Height);  BmpDst.Width:=DstRect.Width;  BmpDst.Height:=DstRect.Height;  BmpDst.Canvas.CopyRect(DstRect,BmpSrc.Canvas,SrcRect);  Clipboard.Assign(BmpDst);  BmpSrc.Free;  BmpDst.Free;end; end. ---------------------------------------------------------------------- taazz · 2017-12-21 17:48 simple graph already supports clipboard in multiple formats, to copy everything you simple call aGraph.CopytoClipboard(false). To copy only the selection you call aGraph.CopytoClipboard(True). As long as you have the cfBitmap in the Clipboardformats property it should copy a bitmap in the clipboard (last time I checked that is). Just a heads up, FormShow is called every time your form regain focus (eg alt+Tab outside your application then back in and the onshow event will be fired) use the OnCreate event for initialization. The code posted hints that the graph is already placed on the form at design time there is no need to create it at run time just set its properties. To add Bitmap support in the copytoclipboard you do something along the lines of 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 Graph.ClipBoardFormats := Graph.ClipBoardFormats+[cfBitmap]; The selectedobjects property holds the current selected objects. ---------------------------------------------------------------------- simone · 2017-12-21 18:04 Thanks Taazz. You are right, but if possible, I'd like to copy a specified rectangular (TRect) area of graph, that could contain partial objects...  For this reason I'm trying a different approach. ---------------------------------------------------------------------- taazz · 2017-12-21 18:12 Quote from: simone on December 21, 2017, 07:04:39 pmThanks Taazz. You are right, but if possible, I'd like to copy a specified rectangular (TRect) area of graph, that could contain partial objects...  For this reason I'm trying a different approach. the default background in lazarus is black not white. after you set the destination bitmap width and height draw a filled rectangle in white (or what ever color the graph is set to) then copy the rect from the source. Keep in mind that big graphs might raise an out of memory exception when creating the bitmap. make sure that copyrect does not re samples the original size to one line and one column less (common problem in windows). ---------------------------------------------------------------------- simone · 2017-12-21 23:23 It seems to me that CopyToClipboard method does not work properly with a scrolled graph. If it is necessary I can provide an example where the problem arises. Thanks again. ---------------------------------------------------------------------- jamie · 2017-12-21 23:36 It may work better if you do SomeForm.GetFormImage for example. From there, you copy from the bitmap not the surface canvas.. Something to try anyways.. ---------------------------------------------------------------------- taazz · 2017-12-22 02:38 Quote from: simone on December 22, 2017, 12:23:37 amIt seems to me that CopyToClipboard method does not work properly with a scrolled graph. If it is necessary I can provide an example where the problem arises. Thanks again. Well yeah makes sense. Bmp canvas did not get the proper treatment. I'm assuming there is a selection involved. I'll have to take a closer look. Thanks for reporting. ---------------------------------------------------------------------- simone · 2017-12-22 07:29 Yes, I have the problem with a selection of objects, after scrolling. Let me know if you need a case test to reproduce the issue. Thanks again! ---------------------------------------------------------------------- taazz · 2017-12-22 07:43 Quote from: simone on December 22, 2017, 08:29:33 amYes, I have the problem with a selection of Object, after scrolling. Let me know if you need a case test to reproduce the issue. Thanks again! the problem has been identified. The solution touches different parts of the library and is not confined in the bitmap and its canvas. It will take me some time to collect and test all the cases(all is a strong word here). I'll try to post a solution when the most used cases are solved. ---------------------------------------------------------------------- simone · 2017-12-24 10:55 Meanwhile I'm trying to work without original CopyToClipboard of TEvsSimpleGraph, but I don't understand why in the following code, the red square drawn in FormCreate is not captured by system clipboard with the call to my method SendToClipboard... Thanks for suggestions! 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 unit Unit1;{$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Clipbrd, USimpleGraph; type   { TForm1 }   TForm1 = class(TForm)    Graph : TEvsSimpleGraph;    procedure FormCreate(Sender: TObject);   private    procedure SendToClipBoard(aGraph : TEvsSimpleGraph; SrcRect : TRect);   publicend; var  Form1: TForm1; implementation{$R *.lfm} procedure TForm1.FormCreate(Sender: TObject);begin  Graph:=TEvsSimpleGraph.Create(Self);  Graph.Align:=alClient;  Graph.Parent:=Self;  Graph.InsertNode(Rect(50,50,100,100)).Pen.Color:=clRed;  SendToClipBoard(Graph,Rect(49,49,111,111));end; procedure TForm1.SendToClipBoard(aGraph : TEvsSimpleGraph; SrcRect : TRect);var  BmpSrc,BmpDst : TBitmap;  DstRect : TRect;begin  BmpSrc:=TBitmap.Create;  BmpDst:=TBitmap.Create;  aGraph.CopyToGraphic(BmpSrc);  DstRect:=Rect(0,0,SrcRect.Width,SrcRect.Height);  BmpDst.Width:=DstRect.Width;  BmpDst.Height:=DstRect.Height;  BmpDst.Canvas.CopyRect(DstRect,BmpSrc.Canvas,SrcRect);  Clipboard.Assign(BmpDst);  BmpSrc.Free;  BmpDst.Free;end; end. ---------------------------------------------------------------------- taazz · 2017-12-24 11:09 The draw method calls owner.gptocp which uses the scrollbar's position to determine if the graph is visible or not, which comes in direct conflict with the code of the bitmap canvas which is already shifted to the correct position. I need to attach a TEvsGraphCanvas to the bitmap and use the canvas to answer the visibility question. Sorry I did not had time to spend on the problem yet, holidays and all that. ---------------------------------------------------------------------- simone · 2017-12-24 11:20 Thanks Taazz for explanations! I thought that in this case the problem was in my code, not in the external component. ---------------------------------------------------------------------- taazz · 2017-12-24 11:38 you can test your code by saving the BmpSrc (before calling copyrect) and BmpDst(after copyrect is called) on disk and check the differences. If there are differences then the most suspect portion would be the pixelformat of the BmpSrc try after calling CopyToGraphic to set the pixel format to pf24bit. ---------------------------------------------------------------------- simone · 2017-12-24 16:23 It seems that something  goes wrong with Canvas.CopyRect... See attached images... ---------------------------------------------------------------------- jamie · 2017-12-24 22:24 I assume you want to be able to send a complete canvas of your work elsewhere and are trying to get it from the GUI surface? If that being the case, you maybe in a world of hurt if the canvas requires a scrollbar to see all of it..  The best solution I have come up with via and app I am currently working on is to make a master procedure or some procedures that accept a Canvas as a parameter on the input.  From there,  you can select a Bitmap as a canvas, a printer.canvas etc. P.S.  there are some strange effects with Tbitmaps with the initial setup..  Create the Bitmap, Set the Pixel format, Width and height then finally  MyNewBitMap.Canvas.Changed;  this creates a proper GDI mapped bitmap... ---------------------------------------------------------------------- simone · 2017-12-24 22:54 Thanks Jamie. I will try to follow your suggestions. ---------------------------------------------------------------------- taazz · 2017-12-26 05:46 Quote from: simone on December 24, 2017, 05:23:26 pmIt seems that something  goes wrong with Canvas.CopyRect... See attached images... there are two things you should pay attention to 1) the BmpDst dimensions look like they are bigger than the bmpsrc dimensions that can create problems. 2) the canvas.copymode might have something to do with the problem. ---------------------------------------------------------------------- simone · 2017-12-26 11:21 Thanks Taazz. The strange thing for me is that with the following code, very similar to the last I posted, where I don't work with the canvas of a TEvsSimpleGraph, but with the canvas of a TForm, SendToClipBoard works properly... For what concerns Canvas.CopyMode, documentation defines it as 'arbitrary number'... So I thought that it exists only for Delphi compatibility 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 unit Unit1;{$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Clipbrd, USimpleGraph; type   { TForm1 }   TForm1 = class(TForm)    Graph : TEvsSimpleGraph;    procedure FormPaint(Sender: TObject);   private    procedure SendToClipBoard(aCanvas : TCanvas; SrcRect : TRect);   publicend; var  Form1: TForm1; implementation{$R *.lfm} procedure TForm1.FormPaint(Sender: TObject);begin  Canvas.Rectangle(50,50,110,110);  SendToClipBoard(Canvas,Rect(49,49,111,111));end; procedure TForm1.SendToClipBoard(aCanvas : TCanvas; SrcRect : TRect);var  BmpSrc,BmpDst : TBitmap;  DstRect : TRect;begin  BmpSrc:=TBitmap.Create;  BmpDst:=TBitmap.Create;  DstRect:=Rect(0,0,SrcRect.Width,SrcRect.Height);  BmpDst.Width:=DstRect.Width;  BmpDst.Height:=DstRect.Height;  BmpDst.Canvas.CopyRect(DstRect,aCanvas,SrcRect);  Clipboard.Assign(BmpDst);  BmpSrc.Free;  BmpDst.Free;end; end. ---------------------------------------------------------------------- simone · 2017-12-26 16:17 Perhaps I understood my mistake: calling copyrect on a canvas of a form inside the oncreate handler... ---------------------------------------------------------------------- taazz · 2017-12-26 16:30 Quote from: simone on December 26, 2017, 05:17:35 pmPerhaps I understood my mistake: calling copyrect on a canvas of a form inside the oncreate handler... What form? the code you shown it draw the graph to a bitmap and then it copy from that bmp canvas to a new bmp canvas. Sorry but it should have no difference if it is called on the create event or a button click. PS: oops I see you are referring to the last code post sorry that code might have different results on creation and after paint yes. ---------------------------------------------------------------------- simone · 2017-12-26 17:33 yes taazz I'm referring to this code. thanks for patience and excellent support! ---------------------------------------------------------------------- taazz · 2017-12-26 18:47 I've tried a couple of things tonight, 1) since the location is not kept in the final bitmap instead the bitmap is as big as needed to encompass all the controls only, I added an extra control at the end to make sure that the coordinates given are inside the bitmap. 2) I save and loaded the bitmap from a memory stream before copying it to the BmpDst to avoid any and all irregularities that simple graph might produce. In both cases the results are the same which drives me to believe that the problem is in the lcl and not the bmp created by the library. I have one last test to run (most likely tomorrow afternoon) to make absolutely sure. Open a 24bit bitmap created from an external application (like mspaint) and use that to copy a portion on the clipboard. If that fails too then the problem is 100% on the lcl not the Simplegraph library, at that point I'll simple post my findings and probably focus on fixing the library letting more experienced developers with the windows api to take a shot at it. PS: After the save and load test I'm fairly certain the problem is in the LCL and most notably in the way it manages images internally but just to be sure I'm going to do that extra test removing the simplegraph library completely from the behavior. Oh and I need to run the tests on 1.6.4 or 1.8 too not only on 1.4.4. There is one last thing you can do assuming that you only wish to copy the rectangle in its original size and no resampling is needed you can use the pixels property to read and set the pixels directly. Its a bit slow but if it works there are other ways to speed it up. ---------------------------------------------------------------------- simone · 2017-12-27 11:18 Thanks for your very accurate analysis. Unfortunately I do not have such a deep knowledge of LCL. For the moment I found a dirty and a bit slow workaround to copy the selected objects in the system clipboard. I create a temporary invisibile graph, where I copy&paste selected objects of main graph. At this point I call ScrollInView(Point(0,0)) on the temporary graph, in order to avoid the scrolling problem. Finally I call CopyToClipboard, again on the temporary graph. It seems to works... ---------------------------------------------------------------------- taazz · 2017-12-27 14:42 Quote from: simone on December 27, 2017, 12:18:46 pmThanks for your very accurate analysis. Unfortunately I do not have such a deep knowledge of LCL. For the moment I found a dirty and a bit slow workaround to copy the selected objects in the system clipboard. I create a temporary invisibile graph, where I copy&paste selected objects of main graph. At this point I call ScrollInView(Point(0,0)) on the temporary graph, in order to avoid the scrolling problem. Finally I call CopyToClipboard, again on the temporary graph. It seems to works... well to gain some speed you can create the hidden SimpleGraph once at start up make sure that it has no controls paste the copied controls and after copy the bitmap delete the controls again. That will probably gain you some minimal speed but not enough. The main problem on the speed of paste is in the paste routine, specifically the routine to search and replace duplicate IDs. I have a modified version that it disregards the existing IDs and replacing them with a unique number from the current auto numbering pool which is a few times faster. In the next update. ---------------------------------------------------------------------- jamie · 2017-12-27 16:41 Please consider using a Timage on the form, send all graphics to that control instead..  Timage contains a bitmap that holds your output, the canvas on the screen can scroll the image in/out as needed to view it but you'll never have an issue when using the Timage as the source for your clipboard.  However, don't expect to drop controls on it and have the controls also appear in your clipboard display. ---------------------------------------------------------------------- simone · 2017-12-28 07:13 I rely on the EvsSimpleGraph component because I have to manage the drawing of custom flow chart diagrams in my application. I'm not sure I can use TImage together with the aforementioned component. However, thanks for the help. ---------------------------------------------------------------------- taazz · 2018-01-04 19:04 an Update has been pushed to github and attached in this message. 1) Minor enhancements have been done to the internal ID management to speed up, load and paste from the clipboard processes. 2) isvisible has been changed to use the enhanced EvsXXXXCanvas classes to decide visibility instead of the owner. Testing so far shows correct results both on copy and drawing, draw speed has not been affected but as it stands now the copy to bitmap can not use any antialised aware canvases (eg GDI+) this will be made available in a later update. ---------------------------------------------------------------------- simone · 2018-01-05 12:17 Thanks Taazz! Now the copy of selected objects to system clipboard works well. ---------------------------------------------------------------------- simone · 2018-01-05 14:55 In your opinion, can your changes compromise backward compatibility? However I integrated the new version of library with my application and all the regression tests has been passed. Moreover I noticed that you have resolved another bug I forget to report, relating to TEvsSimpleGraph.SaveAsBitmap. Before your update, the exported bitmap was cut when the graphic were bigger than visible window. ---------------------------------------------------------------------- taazz · 2018-01-05 15:06 Quote from: simone on January 05, 2018, 03:55:46 pmIn your opinion, can your changes compromise backward compatibility? there is no change in the format or the interface  ( components and unit) that will be incompatible with existing versions. If your old files are loaded and shown correctly then there is no problem with the compatibility. Quote from: simone on January 05, 2018, 03:55:46 pm However I integrated the new version of library with my application and all the regression tests has been passed. Moreover I noticed that you have resolved another bug I forget to report, relating to TEvsSimpleGraph.SaveAsBitmap. Before your update, the exported bitmap was cut when the graphic were bigger than visible window. same bug, different take.