Search code examples
c#clipboardidataobject

Check if clipboard contains image or a file that is an image


I would like to check if the clipboard contains an image, or a file which is in an image format. I want to do it something like this:

    private void myMethod()
    {
        //Check if the clipboard contains an image or a file, that is in image format.
        if (IsClipboardImage())
        {
            //Do important code
        }
        else
        {
            //Do nothing
        }
    }
    private bool IsClipboardImage()
    {
        if (Clipboard.ContainsImage())
            return true;
        else if ( /* code to check if is an image file? */ )
            return true;
        else
            return false;
    }

I have been told to use IDataObject, but using that - how would I check if it's a file that is an image?

My code works if you right click > copy an image from the web, but if it's from my documents, it doesn't work. Any help would be appreciated

Thanks


Solution

  • I think you can use a this way. As explained on that link, there is no build in way to resolve this you have to try and load the image. if it works it's an image if not ignore. Hope this helps