Recent

Author Topic: [SOLVED] paste an image from the clipboard  (Read 733 times)

bourbon

  • New Member
  • *
  • Posts: 28
[SOLVED] paste an image from the clipboard
« on: June 19, 2026, 04:17:18 pm »
Hello,
I can't paste an image from the clipboard. The image I want to paste is a .jpg file.

mes uses :
Below are the units that were originally defined.
Code: Pascal  [Select][+][-]
  1.   Classes, SysUtils, DB, dbf, Forms, Controls, Graphics, Dialogs, DBGrids,
  2.   DBCtrls, Menus, StdCtrls, ExtCtrls, LCLType, ExtDlgs,
  3.   fpimage, ClipBrd, zoom,
  4. Below are the units I added one by one to test my program.
  5.   Variants, Messages, DOS, LConvEncoding, ComCtrls, character;
  6.    ( "zoom" is another unit of my program )
  7.  


my procédure :
Code: Pascal  [Select][+][-]
  1. // --------------------------------------------------------------------
  2. //  IMAGES | popup menu | Coller image depuis le presse-papier
  3. // --------------------------------------------------------------------
  4. procedure TForm1.Coller_ImageClick(Sender: TObject);
  5. var
  6.   I : integer;
  7. begin
  8.   // Colle l'image à partir du presse-papier
  9.  
  10. // Below are the two instructions I added to view the contents of "Formats".
  11.    for I := 0 to Clipboard.FormatCount - 1 do
  12.   Memo2.Lines.Add(IntToStr(Clipboard.Formats[I]));
  13.  
  14.   if Clipboard.HasFormat(CF_PICTURE) then //return true if yes
  15.     begin
  16.       Image1.Picture.Assign(Clipboard);
  17.       Clipboard.free;
  18.       Btn_ImgEnreg.Enabled := True;
  19.       if (WFN_t > 0) then
  20.       begin
  21.         Btn_ImgAjout.Enabled := True;
  22.         Btn_ImgInserer.Enabled := True;
  23.       end;
  24.     end
  25.     else ShowMessage('Pas d''image dans le presse-papier');
  26. end;

Here is the result of the two added instructions:

Memo2
139
136
138
126
141
125
31
124
71
0
0
49
327218704
12884901890
4
0
1
33

Is this normal?
I'm missing something. I copied this code from another program I've been using for many years without any problems.
« Last Edit: July 03, 2026, 10:14:54 pm by bourbon »

paweld

  • Hero Member
  • *****
  • Posts: 1678
Re: paste an image from the clipboard
« Reply #1 on: June 19, 2026, 06:18:30 pm »
Code: Pascal  [Select][+][-]
  1.     // --------------------------------------------------------------------
  2.     //  IMAGES | popup menu | Coller image depuis le presse-papier
  3.     // --------------------------------------------------------------------
  4.     procedure TForm1.Coller_ImageClick(Sender: TObject);
  5.     var
  6.       I : integer;
  7.     begin
  8.       // Colle l'image à partir du presse-papier
  9.      
  10.     // Below are the two instructions I added to view the contents of "Formats".
  11.        for I := 0 to Clipboard.FormatCount - 1 do
  12.       Memo2.Lines.Add(IntToStr(Clipboard.Formats[I]));
  13.      
  14.       if Clipboard.HasPictureFormat then //if there is image (any format) in the clipboard
  15.         begin
  16.           Image1.Picture.LoadFromClipboardFormat(Clipboard.FindPictureFormatID);  //load an image from the clipboard in a specific format
  17.           ////Clipboard.free;  //<- delete this line!!! You cannot free the clipboard yourself - it will cause an error
  18.           //If you want to clear the clipboard, you must use the `Clear` method
  19.           Btn_ImgEnreg.Enabled := True;
  20.           if (WFN_t > 0) then
  21.           begin
  22.             Btn_ImgAjout.Enabled := True;
  23.             Btn_ImgInserer.Enabled := True;
  24.           end;
  25.         end
  26.         else ShowMessage('Pas d''image dans le presse-papier');
  27.     end;
Best regards / Pozdrawiam
paweld

bourbon

  • New Member
  • *
  • Posts: 28
Re: paste an image from the clipboard
« Reply #2 on: June 19, 2026, 09:44:04 pm »
Thank you for your reply.

- I copied your code exactly as it was, but it didn't change anything.

- I removed the test before copying it to the image. Of course, I'm skipping the instructions, but I'm getting the following message.


Regarding the "clipboard.free" command, I had just added it because, while searching the forum, one of the contributors advised not to forget to free the clipboard.

A few clarifications:
1- When I right-click on an image and select "copy" using Ctrl + C, the image goes to the clipboard correctly. To verify this, I open my word processor, then press Ctrl + V, and the image inserts normally.

2- Is the content of the formats in the "Clipboard.Formats" table normal? I was expecting to get a list of possible file extensions.
« Last Edit: June 19, 2026, 10:05:39 pm by bourbon »

paweld

  • Hero Member
  • *****
  • Posts: 1678
Re: paste an image from the clipboard
« Reply #3 on: June 20, 2026, 08:08:47 am »
I haven't encountered this error on my end. Take a look at the project in the attachment, which lists the clipboard contents and shows the selected image format - maybe that will make it easier to find the cause
Best regards / Pozdrawiam
paweld

wp

  • Hero Member
  • *****
  • Posts: 13628
Re: paste an image from the clipboard
« Reply #4 on: June 20, 2026, 02:47:38 pm »
Here is the code used by the Lazarus GraphicEditor (Component Editor of the Picture property of TImage) ((lazarus)/components/ideintf/graphpropedit.pas):

Pasting clipboard into the TImage of the GraphicEditor:
Code: Pascal  [Select][+][-]
  1. procedure TGraphicPropertyEditorForm.PasteActionExecute(Sender: TObject);
  2. begin
  3.   ImagePreview.Picture.Assign(Clipboard);
  4. end;

Enabling/Disabling the button with the "Paste" command:
Code: Pascal  [Select][+][-]
  1. procedure TGraphicPropertyEditorForm.PasteActionUpdate(Sender: TObject);
  2. begin
  3.   PasteAction.Enabled := Clipboard.HasPictureFormat;
  4. end;

Copying contents of the GraphicEditor's image to the clipboard:
Code: Pascal  [Select][+][-]
  1. procedure TGraphicPropertyEditorForm.CopyActionExecute(Sender: TObject);
  2. begin
  3.   Clipboard.Assign(ImagePreview.Picture.Graphic);
  4. end;

Enabling/disabling the button with "Copy" command:
Code: Pascal  [Select][+][-]
  1. procedure TGraphicPropertyEditorForm.CopyActionUpdate(Sender: TObject);
  2. begin
  3.   CopyAction.Enabled := ImagePreview.Picture.Graphic <> nil;
  4. end;

I am not aware of any reports that this sequence is not working.
« Last Edit: June 20, 2026, 02:51:29 pm by wp »

bourbon

  • New Member
  • *
  • Posts: 28
Re: paste an image from the clipboard
« Reply #5 on: July 02, 2026, 07:33:05 am »
Hello,
Sorry for the silence. It is really very hot here (44°C in the shade, and no air conditioning in the accommodation).
I am going to get back to the problem. So far, none of the solutions found or suggested have worked. I will keep you updated on the progress of my troubleshooting later.
Thanks.

bourbon

  • New Member
  • *
  • Posts: 28
Re: paste an image from the clipboard
« Reply #6 on: July 02, 2026, 11:27:14 pm »
Hi,
I can't see what's wrong with this program. I've tested it using all the commands I found online and on the Lazarus wiki https://wiki.lazarus.freepascal.org/Clipboard, but it still crashes at the `if (Clipboard.HasFormat(CF_BITMAP)) ...` check.
Code: Pascal  [Select][+][-]
  1. // --------------------------------------------------------------------------
  2. // Image | Coller
  3. // --------------------------------------------------------------------------
  4. procedure TForm1.Pm_CollerClick(Sender: TObject);
  5. begin
  6.   // Colle l'image à partir du presse-papier
  7.    //if (Clipboard.HasFormat(CF_BITMAP)) or (Clipboard.HasFormat(CF_PICTURE)) then
  8.    if (Clipboard.HasFormat(CF_BITMAP)) then
  9.       Image1.Picture.Assign(Clipboard)
  10.     else
  11.       ShowMessage('Pas d''image dans le presse-papier');
  12. end;
I replicated the exact procedure I use in another program—one that works perfectly.
During testing, the image copies correctly only about once every 50 attempts. When it fails, the program freezes.
I've tried various copying options—such as taking a screenshot with or without selecting a specific area—but nothing works.

I created a minimal test program (included in the zip file below) containing only the bare essentials: right-clicking the panel opens a popup menu with three options. Only the "Copy" option is programmed; the others have no associated events (as they aren't needed just to test the copying issue).

I compared all the settings for the image in the working program against the image in this program. Every setting is strictly identical.

bytebites

  • Hero Member
  • *****
  • Posts: 794
Re: paste an image from the clipboard
« Reply #7 on: July 02, 2026, 11:44:20 pm »
variable  Clipboard is defined in clipbrd unit, you must not define again.

wp

  • Hero Member
  • *****
  • Posts: 13628
Re: paste an image from the clipboard
« Reply #8 on: July 02, 2026, 11:45:41 pm »
Don't declare a variable "Clipboard". Unit ClipBrd contains a function "Clipboard" which creates an instance of the clipboard. No need to create (and destroy) the clipboard yourself. The function "Clipboard" in unit ClipBrd does everything you need.

Simply remove the line
Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   //Clipboard: TClipboard;  // <---- REMOVE
  4.  
and your program will work.
« Last Edit: July 02, 2026, 11:48:45 pm by wp »

bourbon

  • New Member
  • *
  • Posts: 28
Re: paste an image from the clipboard
« Reply #9 on: July 03, 2026, 08:08:14 am »
Other:
//Clipboard: TClipboard; // <---- DELETE
Indeed. I added this instruction out of desperation, after seeing it in an example online. But it doesn't change the program's malfunction.
(This instruction isn't in my other program.)

For paweld
I compiled your program on my PC. Its behavior is inconsistent on my system. When I click the button after copying an picture to the clipboard:
- The program closes immediately, without any message.
- No image appears, but a message shows up at the bottom of the form. It is impossible to close the program. (see picture below)
I had doubts about the clipboard content, so I opened LibreOffice Writer and performed a "paste." The selected and copied image pastes correctly into Writer.

My Config:
Operating System: Linux Mint 22.1                 
          Kernel: Linux 6.8.0-106-generic
    Architecture: x86-64
 Hardware Vendor: ASRock
  Hardware Model: B450 Pro4
Firmware Version: P4.50
CPU Modèle     AMD Ryzen 5 3600

Lazarus      V 4.6
FPC      3.2.2

paweld

  • Hero Member
  • *****
  • Posts: 1678
Re: paste an image from the clipboard
« Reply #10 on: July 03, 2026, 10:48:05 am »
The memo on the left should display information about the data stored in the clipboard - can you paste it?
Best regards / Pozdrawiam
paweld

wp

  • Hero Member
  • *****
  • Posts: 13628
Re: paste an image from the clipboard
« Reply #11 on: July 03, 2026, 01:18:58 pm »
Other:
//Clipboard: TClipboard; // <---- DELETE
This instruction isn't in my other program.
Are you 100% sure? Hold the Ctrl key down and left-click on the word "Clipboard" in the line which triggers the issue. The IDE will open for you the file in which this identifier is declared. It must be in unit "clipbrd", the line with "function Clipboard: TClipboard", otherwise you have a spurious clipboard which must be removed.

bourbon

  • New Member
  • *
  • Posts: 28
Re: paste an image from the clipboard
« Reply #12 on: July 03, 2026, 10:14:18 pm »
Hello,
Eureka! It works fine.
I finally figured out what was wrong:
After struggling with this problem for so long, I decided to test compiling my program using a system (Mint 22.3) booted live from a USB drive. I installed Synaptic via the console, and then used it to install the version of Lazarus available in the repositories.
Then, I launched Lazarus, loaded my program, compiled, and tested it: it works perfectly.

I think I have the explanation:
I work with Mint 22.1. I was having issues with the Lazarus version installed via Synaptic (every time I added a component, Lazarus would crash with a message like "the unit corresponding to this component does not exist"—I'm not 100% sure of the exact wording).
So, I decided to test the latest available version of Lazarus (v4.6.0.0). I completely removed the version installed via Synaptic and then installed v4.6 via the console.
There are almost certainly leftover files from the old version somewhere that are interfering with the operation of this new version.
Thank you for your time.

 

TinyPortal © 2005-2018