Determine the format of an image file?

前端 未结 2 1383
感情败类
感情败类 2021-01-04 08:38

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

相关标签:
2条回答
  • 2021-01-04 09:23

    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");
    
    0 讨论(0)
  • 2021-01-04 09:28

    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.

    0 讨论(0)
提交回复
热议问题