I am writing a program which grabs JPEG images from a camera which is connected via Serial Port. I want to programmatically define an image in WPF, in order to display it once it is retreived, using the Source attribute of Image.
I have found that
image1.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + @"/" + photo + ".jpg"));
Is working absolutely fine with most images, but for some reason it doesn’t like the images from this camera. Can anyone who knows more than me about JPEG please take a look at it and see why it might be a problem?
http://dl.dropbox.com/u/2434899/0%20%282%29.jpg
EDIT: After some investigation, I have determined that it is probably the JFIF header data that WPF/C# does not like. Is there any easy way to correct it in-program?
EDIT 2: I believe I have found the problem. The camera’s JFIF header data is complete junk. It’s basically just 0-10… Had to read it with a Hex editor in the end. I copied across the JFIF header from the working file and now it is fine. Just got to edit each header as it comes in, I guess.
For the information of anyone who encounters this problem with this camera:
http://www.4dsystems.com.au/prod.php?id=75
It seems to give out the same junk header information with each photo:
FF D8 FF E0 00 11 4A 46 49 46 00 01 02 03 04 05 06 07 08 09 0A FF DB
And none of it matches with anything in the JFIF standard:
http://en.wikipedia.org/wiki/JPEG_File_Interchange_Format
It seems that if you just replace the JFIF header with this:
FF D8 FF E0 00 10 4A 46 49 46 00 01 01 01 00 60 00 60 00 00 FF DB
It will work. Just read the file up until the “FF DB” which is also in the junk header – it seems to be the ‘stop word’ for the header – and then swap out that for this.
There is a second lot of what would appear to be header information which continues on until byte 26E… and it seems to be the same in both of the images I have here, but it doesn’t seem to be the cause of the C# loading issue. It doesn’t look like anything in JFIF, so I am not sure.
I’m sorry to say, but I think this image is “corrupt.” I think the image stream that save this image didn’t close/dispose properly. I loaded the image in XAML and got the error:
Exception from HRESULT: 0x88982F72
Then I opened the image in Paint/Photoshop and re-saved it. I opened the re-saved image in XAML and no error. It looks like Photoshop read it fine and when it saved it saved it correctly