Try to create a bit of the GDI + file from the file. If the creation of the Bitmap object fails, you can assume that the image is damaged. GDI + supports several formats: BMP, GIF, JPEG, Exif, PNG, TIFF.
Something like this function should work:
public bool IsValidGDIPlusImage(string filename) { try { using (var bmp = new Bitmap(filename)) { } return true; } catch(Exception ex) { return false; } }
You can limit Exception only ArgumentException , but first I experiment with this before making the switch.
EDIT
If you have byte[] then this should work:
public bool IsValidGDIPlusImage(byte[] imageData) { try { using (var ms = new MemoryStream(imageData)) { using (var bmp = new Bitmap(ms)) { } } return true; } catch (Exception ex) { return false; } }
source share