Recent

Author Topic: [SOLVED] How to save a Graphic from a TPicture in the Clipboard (Linux problem)?  (Read 1724 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 891
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][+][-]
  1. procedure save_to_Clipboard;
  2.    var PT3: TPicture; {is created and free'd externally}
  3.    begin
  4.    try
  5.       PT3.LoadFromFile(ParamStr(1));
  6.       ...
  7.       PT3.SaveToClipboardFormat(CF_Bitmap);        // alternative #1
  8. //    PT3.Bitmap.SaveToClipboardFormat(CF_Bitmap); // alternative #2
  9. //    Clipboard.Assign(PT3.Bitmap);                // alternative #3
  10.  
  11.    except
  12.       on E:Exception do
  13.          begin
  14.          writeln('Error: ', E.Message);
  15.          end;
  16.    end; {try}
  17.    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.
« Last Edit: December 11, 2024, 05:18:14 pm by Hartmut »

wp

  • Hero Member
  • *****
  • Posts: 12531
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #1 on: December 10, 2024, 05:58:45 pm »
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][+][-]
  1. uses
  2.   LCLType, ClipBrd;
  3.  
  4. { TForm1 }
  5.  
  6. procedure TForm1.Button1Click(Sender: TObject);
  7. begin
  8.   Clipboard.Assign(Image1.Picture);
  9. end;
  10.  
  11. procedure TForm1.Button2Click(Sender: TObject);
  12. var
  13.   id: TClipboardFormat;
  14. begin
  15.   id := Clipboard.FindPictureFormatID;
  16.   Image2.Picture.LoadFromClipboardFormat(id);
  17. end;

Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #2 on: December 10, 2024, 07:18:17 pm »
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][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.    begin
  3.    Picture1:=TPicture.Create;
  4.    Picture1.LoadFromFile('Logo_VKM.bmp');
  5.    Clipboard.Assign(Picture1);
  6.    Picture1.Free;
  7.    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][+][-]
  1.    Picture1.LoadFromFile('Logo_VKM.bmp');
  2.    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.
« Last Edit: December 10, 2024, 07:22:12 pm by Hartmut »

jeremiah

  • New Member
  • *
  • Posts: 18
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #3 on: December 10, 2024, 08:11:54 pm »
I am using Debian 12 with Cinnamon here and this works. Quick and dirty...

Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #4 on: December 11, 2024, 11:25:55 am »
Thanks jeremiah. You use exactly the same code as wp in reply #1:
Code: Pascal  [Select][+][-]
  1. 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

  • Hero Member
  • *****
  • Posts: 12531
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #5 on: December 11, 2024, 11:44:08 am »
Which picture type did you load into the picture?

Zvoni

  • Hero Member
  • *****
  • Posts: 2800
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #6 on: December 11, 2024, 11:53:48 am »
Which 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)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

wp

  • Hero Member
  • *****
  • Posts: 12531
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #7 on: December 11, 2024, 12:42:49 pm »
Which picture type did you load into the picture?
apparently a BMP.
But why does the error message mention a PNG?

Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #8 on: December 11, 2024, 12:44:16 pm »
Which 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.

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):"

I added the following procedure to my real project

Code: Pascal  [Select][+][-]
  1. procedure save_Clipboard2;
  2.    var Picture1: TPicture;
  3.    begin
  4.    Picture1:=TPicture.Create;
  5.    Picture1.LoadFromFile('Logo_VKM.bmp');
  6.    if Picture1 <> nil then Clipboard.Assign(Picture1);
  7.    Picture1.Free;
  8.    end;

but this fails with the same message (shown above) on the console.

Quote
Widget 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?
« Last Edit: December 11, 2024, 12:46:26 pm by Hartmut »

Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #9 on: December 11, 2024, 12:50:13 pm »
Which 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

  • Hero Member
  • *****
  • Posts: 2800
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #10 on: December 11, 2024, 01:00:52 pm »
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][+][-]
  1. procedure TClipboard.Assign(Source: TPersistent);
  2. begin
  3.   if Source is TPicture then
  4.     AssignPicture(TPicture(Source))
  5.   else if Source is TGraphic then
  6.     AssignGraphic(TGraphic(Source))
  7.   else
  8.     inherited Assign(Source);
  9. 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:
Quote
png 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?
« Last Edit: December 11, 2024, 01:06:55 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #11 on: December 11, 2024, 01:44:21 pm »
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.

i 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).

Quote
Can 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?

Quote
Quote
png 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

  • Hero Member
  • *****
  • Posts: 2800
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #12 on: December 11, 2024, 01:49:55 pm »
Quote
Can 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
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #13 on: December 11, 2024, 02:16:40 pm »
Something 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][+][-]
  1. procedure save_Clipboard2;
  2.    var Picture1: TPicture;
  3.    begin
  4.    Picture1:=TPicture.Create;
  5.    Picture1.LoadFromFile('Logo_VKM.bmp');
  6.    writeln(Picture1.Graphic.MimeType);
  7.    if Picture1 <> nil then Clipboard.Assign(Picture1);
  8.    writeln(Picture1.Graphic.MimeType);
  9.    Picture1.Free;
  10.    end;

and the output was:

Code: Text  [Select][+][-]
  1. image/bmp
  2. (png:23045): Gtk-CRITICAL **: 14:12:08.456: IA__gtk_selection_owner_set: assertion 'widget == NULL || gtk_widget_get_realized (widget)' failed
  3. image/bmp

Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: How to save a Graphic from a TPicture into the Clipboard (Linux problem)?
« Reply #14 on: December 11, 2024, 02:46:02 pm »
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:

Which 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.

 

TinyPortal © 2005-2018