[SOLVED] How to save a Graphic from a TPicture in the Clipboard (Linux problem)? lazarus / 39-Graphics ---------------------------------------------------------------------- Hartmut · 2024-12-08 16:57 My program loads a graphic file (BMP or PNG or JPG or GIF) into a TPicture. Then I want to be able to save this graphic into the Clipboard. The following code works on Windows, but not on Linux: 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 save_to_Clipboard;   var PT3: TPicture; {is created and free'd externally}   begin   try      PT3.LoadFromFile(ParamStr(1));      ...      PT3.SaveToClipboardFormat(CF_Bitmap);        // alternative #1//    PT3.Bitmap.SaveToClipboardFormat(CF_Bitmap); // alternative #2//    Clipboard.Assign(PT3.Bitmap);                // alternative #3    except      on E:Exception do         begin         writeln('Error: ', E.Message);         end;   end; {try}   end; In this forum I found 3 different ways to do so (see above). All 3 work on Windows, but on Linux always the following message is written to the console which I don't understand and nothing is stored into the Clipboard: (png:2268): Gtk-CRITICAL **: 17:32:35.856: IA__gtk_selection_owner_set: assertion 'widget == NULL || gtk_widget_get_realized (widget)' failed I'm still a beginner to Graphics and to the Clipboard regarding Graphics and don't really know what a widget is. Must I initialize something additionally on Linux? Versions:  - Windows 7 with Lazarus 3.6.0 and 2.0.10  - Linux Ubuntu 22.04 with Lazarus 3.4.0 and 2.0.10 GTK2 Thanks in advance. ---------------------------------------------------------------------- wp · 2024-12-10 16:58 This works for me (Kubuntu 24.04, Laz/main, FPC 3.2.2, gtk2; as well as Manjaro, Laz 3.4, FPC 3.2.2, gtk2, qt5, qt6): 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  LCLType, ClipBrd; { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  Clipboard.Assign(Image1.Picture);end; procedure TForm1.Button2Click(Sender: TObject);var  id: TClipboardFormat;begin  id := Clipboard.FindPictureFormatID;  Image2.Picture.LoadFromClipboardFormat(id);end; ---------------------------------------------------------------------- Hartmut · 2024-12-10 18:18 Thank you very much wp for your reply. The good news is: in a separate project does it work, even if I change it to: 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   Picture1:=TPicture.Create;   Picture1.LoadFromFile('Logo_VKM.bmp');   Clipboard.Assign(Picture1);   Picture1.Free;   end; But in my real project, even if I put these 2 lines directly one after another ('create' and 'free' are separate): 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    Picture1.LoadFromFile('Logo_VKM.bmp');   Clipboard.Assign(Picture1); it does not work: nothing is stored into the Clipboard, but the following message is written to the console: (png:10977): Gtk-CRITICAL **: 18:17:30.346: IA__gtk_selection_owner_set: assertion 'widget == NULL || gtk_widget_get_realized (widget)' failed Very strange... I fear that I must reduce my real project (5400 lines of code plus many common units) peace by peace, until I see a difference :-(( To do this more constructive / straightforward: does anyone have an idea, what the problem could be? Maybe a Unit of the LCL or FPC which might disturb? Or can someone guess from this console message, where the problem could be: (png:10977): Gtk-CRITICAL **: 18:17:30.346: IA__gtk_selection_owner_set: assertion 'widget == NULL || gtk_widget_get_realized (widget)' failed As said, on Windows it works in my real project, only on Linux I have this problem. ---------------------------------------------------------------------- jeremiah · 2024-12-10 19:11 I am using Debian 12 with Cinnamon here and this works. Quick and dirty... ---------------------------------------------------------------------- Hartmut · 2024-12-11 10:25 Thanks jeremiah. You use exactly the same code as wp in reply #1: 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 clipboard.Assign(Image1.Picture); As I wrote, this works for me only in a separate project, but not in my real project, where I get this message on the console instead: (png:10977): Gtk-CRITICAL **: 18:17:30.346: IA__gtk_selection_owner_set: assertion 'widget == NULL || gtk_widget_get_realized (widget)' failed Does anybody know, what this message means? Is this a message from the LCL / FPC or from my Linux OS? ---------------------------------------------------------------------- wp · 2024-12-11 10:44 Which picture type did you load into the picture? ---------------------------------------------------------------------- Zvoni · 2024-12-11 10:53 Quote from: wp on December 11, 2024, 11:44:08 amWhich picture type did you load into the picture? apparently a BMP. @Hartmut: The only difference i see between your sucessful Test and your real project (at least what i see): You explicitely Create/Free the TPicture in your sucessful Test. i would check this here: "('create' and 'free' are separate):" Widget is basically a "Control" and the Error-Message apparently checks if a widget/Control is NULL (as in non-existant) ---------------------------------------------------------------------- wp · 2024-12-11 11:42 Quote from: Zvoni on December 11, 2024, 11:53:48 amQuote from: wp on December 11, 2024, 11:44:08 amWhich picture type did you load into the picture? apparently a BMP. But why does the error message mention a PNG? ---------------------------------------------------------------------- Hartmut · 2024-12-11 11:44 Quote from: wp on December 11, 2024, 11:44:08 amWhich picture type did you load into the picture? I tried BMP, PNG and JPG. All 3 work in a separate project, but all 3 fail in my real project with the same message on the console. Quote from: Zvoni on December 11, 2024, 11:53:48 amThe only difference i see between your sucessful Test and your real project (at least what i see): You explicitely Create/Free the TPicture in your sucessful Test. i would check this here: "('create' and 'free' are separate):" I added the following procedure to my real project 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 save_Clipboard2;   var Picture1: TPicture;   begin   Picture1:=TPicture.Create;   Picture1.LoadFromFile('Logo_VKM.bmp');   if Picture1 <> nil then Clipboard.Assign(Picture1);   Picture1.Free;   end; but this fails with the same message (shown above) on the console. QuoteWidget is basically a "Control" and the Error-Message apparently checks if a widget/Control is NULL (as in non-existant) Does this mean, that this message is written by the LCL? Would it help to find the source code where it is written? ---------------------------------------------------------------------- Hartmut · 2024-12-11 11:50 Quote from: wp on December 11, 2024, 12:42:49 pmQuote from: Zvoni on December 11, 2024, 11:53:48 amQuote from: wp on December 11, 2024, 11:44:08 amWhich picture type did you load into the picture? apparently a BMP. But why does the error message mention a PNG? png is the name of the project and png.pas is the name of the main module. ---------------------------------------------------------------------- Zvoni · 2024-12-11 12:00 Apparently there was the same error some 9 years ago in regards to SynEdit: https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/28679 i just looked at the source of TClipBoard.Assign 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 TClipboard.Assign(Source: TPersistent);begin  if Source is TPicture then    AssignPicture(TPicture(Source))  else if Source is TGraphic then    AssignGraphic(TGraphic(Source))  else    inherited Assign(Source);end;hmm....maybe there is something fishy about the Format you pass in Picture1 Can you check the MimeType of your Picture? Looking further through the Source, Clipboard.Assign(TPicture) gets forwarded to Clipboard.AssignPicture(TPicture), which gets forwarded to Clipboard.AssignGraphic(Source.Graphic). And there it checks the MimeType and tries to register it Maybe something fails there EDIT: Quotepng is the name of the project and png.pas is the name of the main module.is then (png:10977) the line-number in your pas-file? ---------------------------------------------------------------------- Hartmut · 2024-12-11 12:44 Thank you Zvoni for your reply and your help. The error in regards to SynEdit is still open and had to do with the clipboard too. Quote from: Zvoni on December 11, 2024, 01:00:52 pmi just looked at the source of TClipBoard.Assign ... hmm....maybe there is something fishy about the Format you pass in Picture1 My 3 demo files (each BMP, PNG and JPG) all work on Linux, when I run the code in a separate project. Only when I run the code in my real project, then all 3 fail. So I assume, the 3 demo files are ok (and on Windows, all 3 work in my real project). QuoteCan you check the MimeType of your Picture? I'm not sure how to do this. I attached a screenshot of the properties of my BMP demo file shown by filemanager Nautilus. Did you mean that? QuoteQuotepng is the name of the project and png.pas is the name of the main module.is then (png:10977) the line-number in your pas-file? This number seems to increase each time the message is shown, but it can't be a line number, because main file png.pas has only 28 lines and Unit picview.pas, which contains above procedure 'save_Clipboard2' contains only about 5400 lines. ---------------------------------------------------------------------- Zvoni · 2024-12-11 12:49 Quote from: Hartmut on December 11, 2024, 01:44:21 pmQuoteCan you check the MimeType of your Picture? I'm not sure how to do this. I attached a screenshot of the properties of my BMP demo file shown by filemanager Nautilus. Did you mean that? Something along the lines of Picture1.Graphic.MimeType do a Writeln with it or assign it to a Label-Caption ---------------------------------------------------------------------- Hartmut · 2024-12-11 13:16 Quote from: Zvoni on December 11, 2024, 01:49:55 pmSomething along the lines of Picture1.Graphic.MimeType do a Writeln with it or assign it to a Label-Caption I changed my procedure to 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 save_Clipboard2;   var Picture1: TPicture;   begin   Picture1:=TPicture.Create;   Picture1.LoadFromFile('Logo_VKM.bmp');   writeln(Picture1.Graphic.MimeType);   if Picture1 <> nil then Clipboard.Assign(Picture1);   writeln(Picture1.Graphic.MimeType);   Picture1.Free;   end; and the output was: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i image/bmp(png:23045): Gtk-CRITICAL **: 14:12:08.456: IA__gtk_selection_owner_set: assertion 'widget == NULL || gtk_widget_get_realized (widget)' failedimage/bmp ---------------------------------------------------------------------- Hartmut · 2024-12-11 13:46 Seems that the problem is solved. Now my 3 demo files work in my real project on Linux too. It was the question from wp: Quote from: Hartmut on December 11, 2024, 12:50:13 pmQuote from: wp on December 11, 2024, 12:42:49 pmQuote from: Zvoni on December 11, 2024, 11:53:48 amQuote from: wp on December 11, 2024, 11:44:08 amWhich picture type did you load into the picture? apparently a BMP. But why does the error message mention a PNG? png is the name of the project and png.pas is the name of the main module. which pointed me to have a look at the small main module png.pas. There I used an uncommon way to start my program. Obviously this caused the problem. Probably I did it wrong (or we found a bug which only then occurs and only on Linux, because on Windows it worked). Details will follow, but I must have a break now. ---------------------------------------------------------------------- Zvoni · 2024-12-11 13:47 hmmm.... As a Test: 1) Try your Save to Clipboard (That error should show) 2) Try to do a CTRL+V somewhere else (say in gimp or wherever) 3) Try your Save to Clipboard again --> Does the error occur again? Otherwise i'm out of ideas ---------------------------------------------------------------------- wp · 2024-12-11 14:04 Quote from: Hartmut on December 11, 2024, 01:44:21 pmbecause main file png.pas has only 28 lines This seems to indicate that you are not writing a standard LCL GUI application. In this case you probably did not have the unit InterfaceBase (or Interfaces?) in the uses clause and thus did not initialize the widgetset-based access to the clipboard. ---------------------------------------------------------------------- Hartmut · 2024-12-11 16:17 As I assumed, there was something wrong in the kind in which I started my program. To find out, what I must change, I created a new Topic and a small demo project: https://forum.lazarus.freepascal.org/index.php/topic,69574.0.html I close this Topic now. Thanks a lot to wp and Zvoni for your help.