简体   繁体   中英

C# Clipboard.ContainsImage() is working differently while copying file from file website and from desktop

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM