7

The user right clicks on a file(say on the desktop) and clicks 'copy' . Now how do I determine in C# if the file copied to the clipboard is an image type ?

Clipboard.ContainsImage() doesn't work in this case

The following determines if an image is directly copied to the clipboard, not if a file is copied to the clipboard

   IDataObject d = Clipboard.GetDataObject();

   if(d.GetDataPresent(DataFormats.Bitmap))
   {
       MessageBox.Show("image file found");
   }

To be clear I want to determine if the 'file' copied to the clipboard is an image.

Edit: the answers are great, but how do I get the filename of a file copied to the clipboard ? Clipboard.getText() doesn't seem to work.. Edit2: Clipboard.GetFileDropList() works

gyaani_guy
  • 3,191
  • 8
  • 43
  • 51
  • 1
    In many cases, checking file extension is enough. However you can both use `Magic-Bytes` method (as CodeInChaos said) and `Exception Handling` method(as Shekhar_Pro said). Also there is a tool called `TrID` which is a free command-line utility that can be used to determine file types using a signature database. http://mark0.net/soft-trid-e.html – fardjad Jan 23 '11 at 16:42

3 Answers3

7

You can check it like this (There is no built in way of doing this) Read the file and use it in a Graphics Image Object if it will be image it will work fine else it will Raise an OutOfMemoryException.

here is a sample code:

 bool IsAnImage(string filename)
  {
   try
    {
        Image newImage = Image.FromFile(filename);
    }
    catch (OutOfMemoryException ex)
    {
        // Image.FromFile will throw this if file is invalid.
       return false;
    }
    return true;
  }

It will work for BMP, GIF, JPEG, PNG, TIFF file formats


Update

Here is the code to get the FileName:

IDataObject d = Clipboard.GetDataObject();
if(d.GetDataPresent(DataFormats.FileDrop))
{
    //This line gets all the file paths that were selected in explorer
    string[] files = d.GetData(DataFormats.FileDrop);
    //Get the name of the file. This line only gets the first file name if many file were selected in explorer
    string TheImageFile = files[0];
    //Use above method to check if file is Image file
    if(IsAnImage(TheImageFile))
    {
         //Process file if is an image
    }
    {
         //Process file if not an image
    }
}
Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
  • this works fine, except I am unable to get the filename of the file copied to the clipboard. any ideas how to get that ? – gyaani_guy Jan 23 '11 at 17:08
  • don't you need to have a cast(ing) of DataFormats.FileDrop to string array? string[] files = (string[]) d.GetData(DataFormats.FileDrop); – Georg Jul 12 '19 at 20:43
3

Get the filename(s) from the clipboard(copying a file to the clip-board just copies its name). Then check if file(s) are image(s).

There are two ways to do that:

  1. By file extension
  2. Open the file and check for magic-bytes indicating the common image formats

I prefer the second one because it works even if a file has the wrong extension. On slow media it might be slower though since you need to access the file instead of just working the filename which you got from the clipboard.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • thanks for pointing me in the right direction . By file extension doesn't seem to work here. I will have to figure out the magic byte thing. – gyaani_guy Jan 23 '11 at 16:35
0

You can easily check the clipboard if contains image or not:

if (Clipboard.ContainsImage())
{
    MessageBox.Show("Yes this is an image.");
}
else
{
    MessageBox.Show("No this is not an image!");
}
UserMat
  • 600
  • 4
  • 10
  • 27