Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the format of an image file?

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

like image 477
C. Ross Avatar asked Feb 15 '11 14:02

C. Ross


People also ask

How do you find out what format a file is?

Right-click the file. Select the Properties option. In the Properties window, similar to what is shown below, see the Type of file entry, which is the file type and extension.

What is the format of a JPG file?

What is a JPG File? JPG is a widely used compressed image format for containing digital images. It is the most common image format used in digital cameras, different operating systems and on the Internet. A compression ratio of 10:1 can be applied in JPG images without losing significant details.


2 Answers

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");
like image 146
Alex Avatar answered Sep 21 '22 18:09

Alex


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.

like image 25
Davide Piras Avatar answered Sep 19 '22 18:09

Davide Piras