tags:

views:

278

answers:

2

I need to be able to validate if a JPEG image is valid and not corrupted. This is not the same as question 210650 which just asks wants to validate that it is, in fact, a JPEG image. Loading from Image with validation eg:

var testJpg = 
  Bitmap.FromStream(filename, useEmbeddedColorManagement, validateData);

will not throw an exception for clipped images. Currently our solution is to try to open the file an external GD script and parse for any errors thrown from gdImageCreateFromJpeg.

gdImagePtr im;
im = gdImageCreateFromJpeg(testFile);
fclose(in);

But there has to be some .Net code somewhere that validates that a JPEG is not only valid, but contains the complete image?

+1  A: 

Taking JPEG reference structure from Wikipedia, maybe you can look for EOI byte from the stream?

Adrian Godong
A: 

There is the pnetlib that provides an interop to jpeglib using c#, once you have that you can validate whatever you want.
You can try to use thier JpegReader, maybe it will give you an error out of the box.

Shay Erlichmen
Any sample code in C# ?? thanks
alhambraeidos