0

I was investigating an interesting thing. I want to get Image from Clipboard in my C# program.

Code sample:

[STAThread]
public Image GetClipboardImage()
{
    MessageBox.Show("try to get image");
    Image returnImage = null;
    if (Clipboard.ContainsImage())
    {
        MessageBox.Show("getting image");
        returnImage = Clipboard.GetImage();
    }
    return returnImage;
}

When I try to get an image that was copied to clipboard from any website the code above works quite good. But it doesn't work when I am copying an image from my computer (I mean desktop for example). Clipboard.ContainsImage() returns false in the second case.

One more difference between copying from web and from desktop: In the first case I cannot paste the image from Clipboard to folder on my computer. Of course in the second case it can be done. It seems that in the second case there is one real image and one file which contains this image. But I am not sure whether it can be.

So, what's the issue in my situation and how I can resolve it?

MrLore
  • 3,759
  • 2
  • 28
  • 36
Anton Kasianchuk
  • 1,177
  • 5
  • 13
  • 31
  • Are you copying the image from the web by right click copying the image and copying an image on the disk by right click copying an image file? I'd imagine the two are different – Charleh Aug 22 '13 at 11:07
  • I tried to copy by right click and by Ctrl+C and I saw no difference between copying from web and copying from disk – Anton Kasianchuk Aug 22 '13 at 11:10
  • I just checked it one more time and there was no difference – Anton Kasianchuk Aug 22 '13 at 11:13
  • Your assumption is right - copying a file puts the reference to a file in the clipboard, while copying an image (from say the web or Paint or something) places the image onto the clipboard. You may want to check if the clipboard contains a string, then check if it's a file that exists, then check if that file is an image. – Bikonja Aug 22 '13 at 13:13
  • Let me clarify. I try to copy not the file of my image but exact image. I mean that first I open the file in Windows Photo Viewer and than I do "right click -> copy" – Anton Kasianchuk Aug 22 '13 at 13:30
  • I tried changing viewers with no success – Anton Kasianchuk Aug 22 '13 at 14:02
  • I have investigated that Clipboard.ContainsFileDropList() returns true while copying from disk – Anton Kasianchuk Aug 23 '13 at 09:51

1 Answers1

0

The next code demonstrate an appropriate solution

IDataObject myDataObject = Clipboard.GetDataObject();
string[] files = (string[])myDataObject.GetData(DataFormats.FileDrop);
MessageBox.Show(files[0]);

You will show massagebox with the full path to the file you have copied to clipboard before.

Anton Kasianchuk
  • 1,177
  • 5
  • 13
  • 31