Reputation: 393
I am writing some code to convert some images stored in a database to JPEG. I need to check if the image isn't JPEG, and all I have is a byte[]
that I am putting into a MemoryStream
. The current code then plugs that into a Bitmap
, which doesn't tell me anything about the image.
Upvotes: 1
Views: 1265
Reputation: 3813
You could check the first few bytes. Most, but not all, JPEG files start with the byte sequence FF D8 FF
. If you need to be sure, you'll need to do a bit more.
I wrote a VB class fifteen years ago to do this. I still find copies of it floating around the web: https://code.google.com/p/vbgore/source/browse/trunk/Code/GrhDatMaker/CImageInfo.cls?spec=svn45&r=45
It's easy enough to read. This should be faster than loading into an Image
class.
Upvotes: 0
Reputation: 149618
Checkout the Image.RawFormat
property. Once you load the image from the stream you could do:
if (ImageFormat.Jpeg.Equals(image.RawFormat))
{
// Image is JPEG
}
Upvotes: 2