Recent

Author Topic: [SOLVED] How do I get an image from clipboard?  (Read 16535 times)

nsunny

  • Full Member
  • ***
  • Posts: 117
  • Code is magic
    • LazPlanet
[SOLVED] How do I get an image from clipboard?
« on: December 23, 2010, 04:36:06 am »
I have looked around the forum but did not find a working code.

Suppose, I have an image on the clipboard. I now want to retrieve the image from the clipboard on a TBitmap. And I want it to be drawn in a TBitmap (say... MyBitmap).

I have tried the following:

FOR COPYING (it worked):
Code: [Select]
 // MyBitmap is our TBitmap to be copied
  Clipboard.Assign(MyBitmap);

FOR PASTING (did not work):
Code: [Select]
if Clipboard.HasFormat(CF_Bitmap) then
begin
  MyBitmap.Assign(Clipboard);
end;

Little help from here: Basic Clipboard Operations (Cut/Copy/Paste) using the TClipboard object
« Last Edit: January 18, 2011, 05:05:12 am by nsunny »
Lazarus TTS Tutorial | LazPlanet
Lazarus 2.2.0 | FPC 3.2.2 | Linux/OpenBSD/ReactOS

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How do I get an image from clipboard?
« Reply #1 on: December 23, 2010, 06:06:31 am »

nsunny

  • Full Member
  • ***
  • Posts: 117
  • Code is magic
    • LazPlanet
Re: How do I get an image from clipboard?
« Reply #2 on: December 23, 2010, 07:14:47 am »
I have tried:

(A)
Code: [Select]
MyBitmap.LoadFromClipboardFormat( Clipboard.FindPictureFormatID );
(B)
Code: [Select]
MyBitmap.LoadFromClipboardFormat( Clipboard.FindFormatID('image/bmp') );
both raises error. :(  :(
Lazarus TTS Tutorial | LazPlanet
Lazarus 2.2.0 | FPC 3.2.2 | Linux/OpenBSD/ReactOS

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2584
Re: How do I get an image from clipboard?
« Reply #3 on: December 23, 2010, 10:12:06 am »
What error ?
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

nsunny

  • Full Member
  • ***
  • Posts: 117
  • Code is magic
    • LazPlanet
Re: How do I get an image from clipboard?
« Reply #4 on: December 24, 2010, 07:17:22 pm »
What error ?

Quote
both raises error.  :( :(

ooups! a little correction...

(A)
Code: [Select]
MyBitmap.LoadFromClipboardFormat( Clipboard.FindPictureFormatID );
The above code raises an error:
Quote
Project ZZZ raised exception class 'EInvalidGraphic' with message:
TBitmap: Unsupported MimeType:


But code (B) does not do anything. As if it was never written.  :o
Lazarus TTS Tutorial | LazPlanet
Lazarus 2.2.0 | FPC 3.2.2 | Linux/OpenBSD/ReactOS

longbow767

  • Newbie
  • Posts: 1

José Mejuto

  • Full Member
  • ***
  • Posts: 136
Re: How do I get an image from clipboard?
« Reply #6 on: January 04, 2011, 07:50:21 pm »
The above code raises an error:
Project ZZZ raised exception class 'EInvalidGraphic' with message:
TBitmap: Unsupported MimeType:
But code (B) does not do anything. As if it was never written.  :o

Hello,

In one of my programs I'm using:

Code: [Select]
ImageToEdit.Picture.LoadFromClipboardFormat(PredefinedClipboardFormat(pcfDelphiBitmap));

It is working for me in Windows.

nsunny

  • Full Member
  • ***
  • Posts: 117
  • Code is magic
    • LazPlanet
Re: How do I get an image from clipboard?
« Reply #7 on: January 17, 2011, 08:19:46 am »
In one of my programs I'm using:

Code: [Select]
ImageToEdit.Picture.LoadFromClipboardFormat(PredefinedClipboardFormat(pcfDelphiBitmap));

It is working for me in Windows.

It worked for me too. Thanks. :)

I want to do something more. I want this code only be executed if there is an image in the clipboard (not when text is in clipboard). I have tried the code below but failed:

Code: [Select]
   if Clipboard.HasFormat(PredefinedClipboardFormat(pcfDelphiBitmap)) then
    begin

        MyBitmap.LoadFromClipboardFormat(PredefinedClipboardFormat(pcfDelphiBitmap));

    end;
« Last Edit: January 17, 2011, 08:28:41 am by nsunny »
Lazarus TTS Tutorial | LazPlanet
Lazarus 2.2.0 | FPC 3.2.2 | Linux/OpenBSD/ReactOS

José Mejuto

  • Full Member
  • ***
  • Posts: 136
Re: How do I get an image from clipboard?
« Reply #8 on: January 17, 2011, 08:12:59 pm »
I want to do something more. I want this code only be executed if there is an image in the clipboard (not when text is in clipboard). I have tried the code below but failed:

Code: [Select]
   if Clipboard.HasFormat(PredefinedClipboardFormat(pcfDelphiBitmap)) then
    begin
 MyBitmap.LoadFromClipboardFormat(PredefinedClipboardFormat(pcfDelphiBitmap));
    end;

Hello,

I'm using:

Code: [Select]
  PictureAvailable:=false;
  if Clipboard.HasFormat(PredefinedClipboardFormat(pcfDelphiBitmap)) then PictureAvailable:=true;
  if Clipboard.HasFormat(PredefinedClipboardFormat(pcfBitmap)) then PictureAvailable:=true;
  if PictureAvailable then begin

nsunny

  • Full Member
  • ***
  • Posts: 117
  • Code is magic
    • LazPlanet
Re: How do I get an image from clipboard?
« Reply #9 on: January 18, 2011, 05:03:54 am »
Hello,

I'm using:

Code: [Select]
  PictureAvailable:=false;
  if Clipboard.HasFormat(PredefinedClipboardFormat(pcfDelphiBitmap)) then PictureAvailable:=true;
  if Clipboard.HasFormat(PredefinedClipboardFormat(pcfBitmap)) then PictureAvailable:=true;
  if PictureAvailable then begin

It worked!!!
Thanks JoshyFun.
Lazarus TTS Tutorial | LazPlanet
Lazarus 2.2.0 | FPC 3.2.2 | Linux/OpenBSD/ReactOS

 

TinyPortal © 2005-2018