Read the image and determine if its root C # file is corrupted

How to determine if the image that I have as source bytes is damaged or not. Is there an openource library that handles this problem for multiple C # formats?

thanks

+6
source share
2 answers

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; } } 
+11
source

You can look at these links to take an idea. The first is here; Confirm Image

And the second is here; How to check corrupted TIFF images

And, unfortunately, I do not know any external library for this.

0
source

Source: https://habr.com/ru/post/905887/


All Articles