Forum > Graphics
[SOLVED] How to save a Graphic from a TPicture in the Clipboard (Linux problem)?
Hartmut:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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:
I am using Debian 12 with Cinnamon here and this works. Quick and dirty...
Hartmut:
Thanks jeremiah. You use exactly the same code as wp in reply #1:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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?
Navigation
[0] Message Index
[#] Next page