How To Check the Image Format in C#


This example C# script is a universal example for how to extract image data from just a byte array. You can extract file type and dimensions, easily.

//check image dimensions 
System.Drawing.Image logoImage = System.Drawing.Image.FromStream(new MemoryStream(imageData)); 
if(logoImage.PhysicalDimension.Height > 100 || logoImage.PhysicalDimension.Width > 80) 
        throw m_ef.CreateException("Incorrect Image Dimensions!");

ImageType imageType; 
//determine file type 
if(logoImage.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)) 
        imageType = ImageType.JPG; 
else if(logoImage.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif)) 
        imageType = ImageType.GIF;

Popular Posts