12

How can I programatically determine the image format of an image file, including the specific encoding such as the TIFF group?

C. Ross
  • 31,137
  • 42
  • 147
  • 238
  • 1
    Take a look at this article: http://www.mikekunz.com/image_file_header.html it explains the header for the most common image files, you'll have to read it and then match it against either what you expect or simply deduce from the header the image format. – Jorge Córdoba Feb 15 '11 at 14:35
  • It's a good article, but it doesn't help with format specific attributes. – C. Ross Feb 15 '11 at 14:38
  • @Ross [If you dont mind digging into the codec you can start here](http://code.google.com/p/fb2pdf/source/browse/trunk/src/java/src/com/itextpdf/text/pdf/codec/TiffImage.java?r=623) – Reno Feb 15 '11 at 14:42
  • @Reno I'm looking at the [official format](http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf). IText will probably be easier, thanks. – C. Ross Feb 15 '11 at 15:10
  • You can see here, very simple solution : [1]http://stackoverflow.com/questions/5209506/how-can-i-know-what-image-format-i-get-from-a-stream – Alban Becquet Mar 29 '13 at 15:53
  • possible duplicate of [Find image format using Bitmap object in C#](http://stackoverflow.com/questions/1397512/find-image-format-using-bitmap-object-in-c-sharp) – C. Ross Jul 05 '13 at 17:00

2 Answers2

4

see my answer here:

Find image format using Bitmap object in C#

using System.Linq;

//...

//get image
var file_bytes = System.Convert.FromBase64String(@"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==");
var file_stream = new System.IO.MemoryStream(file_bytes);
var file_image = System.Drawing.Image.FromStream(file_stream);

//get image format
var file_image_format = typeof(System.Drawing.Imaging.ImageFormat).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static).ToList().ConvertAll(property => property.GetValue(null, null)).Single(image_format => image_format.Equals(file_image.RawFormat));
System.Diagnostics.Debug.WriteLine(file_image_format, "file_image_format");

//get image codec
var file_image_format_codec = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders().ToList().Single(image_codec => image_codec.FormatID == file_image.RawFormat.Guid);
System.Diagnostics.Debug.WriteLine(file_image_format_codec.CodecName + ", mime: " + file_image_format_codec.MimeType + ", extension: " + file_image_format_codec.FilenameExtension, "image_codecs", "file_image_format_type");
Community
  • 1
  • 1
Alex
  • 5,909
  • 2
  • 35
  • 25
  • An annoying side effect is that if the file is NOT an image, then trying to load them from the stream would silently throw a weird OutOfMemory Exception. At least you see them reported when debugging in Visual Studio. It is transparent but annoying to see all those OOM exceptions. – Lord of Scripts Mar 22 '16 at 22:14
1

if you don't find any ready-made library I guess you should open the file as binary and look for the header data, which means you have to know how the header looks like for every format you would like to support.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147