You can check it like this (there is no built-in way to do this) Read the file and use it in the graphic image object, if it is displayed, it will work fine, otherwise 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 in the formats BMP, GIF, JPEG, PNG, TIFF
UpdateHere 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
source share