Recent

Author Topic: [Solved] Picture in the Windows clipboard- how to save it to a file?  (Read 1421 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2713
    • UVviewsoft
User has a picture in clipboard. E.g. it's Win32.
Picture in usual format.
E.g. copied from Windows Paint.
How can one read this picture (from clipboard) and save it to a PNG or JPEG file?
« Last Edit: February 06, 2021, 12:42:48 pm by Alextp »

avk

  • Hero Member
  • *****
  • Posts: 826
Re: Picture in the Windows clipboard- how to save it to a file?
« Reply #1 on: February 05, 2021, 05:44:03 pm »
Something like this?
Code: Pascal  [Select][+][-]
  1. ...
  2. uses
  3.   ..., Clipbrd, ...
  4. ...
  5. procedure TForm1.Button1(Sender: TObject);
  6. begin
  7.   if ClipBoard.HasPictureFormat then
  8.     with TPicture.Create do
  9.       try
  10.         Assign(Clipboard);
  11.         SaveToFile('Picture.png');
  12.       finally
  13.         Free;
  14.       end;
  15. end;
  16.  

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Picture in the Windows clipboard- how to save it to a file?
« Reply #2 on: February 05, 2021, 05:48:16 pm »
Alternatively (cause avk's answer seems too simple :)):

All format-specific images descend from TGraphic so you can probably just create the proper graphic (TPortableNetworkGraphic or TJPEGImage) and use its LoadFromClipboardFormat(), it there is such a format in the clipboard.

If that doesn't work (which is probable) just try the same trick with a compatible format (usually a TBitmap) or a TPicture.

Note that TClipboard has several methods that might help you to "divine" which is the format of the image it contains: FindPictureFormatID, HasPictureFormat, etc.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

AlexTP

  • Hero Member
  • *****
  • Posts: 2713
    • UVviewsoft
Re: [Solved] Picture in the Windows clipboard- how to save it to a file?
« Reply #3 on: February 06, 2021, 12:44:01 pm »
Thank you guys.

 

TinyPortal © 2005-2018