2011-02-15 27 views
11

如何以編程方式確定圖像文件的圖像格式,包括特定的編碼(如TIFF組)?確定圖像文件的格式?

+1

看看這篇文章:http://www.mikekunz.com/image_file_header.html它解釋爲最常見的圖像文件的標題,你必須閱讀它,然後將其與你期望的或者只是從標題中推斷出的圖像格式進行匹配。 – 2011-02-15 14:35:21

+0

這是一篇很好的文章,但它對格式特定的屬性沒有幫助。 – 2011-02-15 14:38:27

回答

4

在這裏看到我的回答:

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"); 
1

如果你沒有找到任何現成的庫我想你應該打開文件作爲二進制文件,並尋找標題數據,這意味着你必須知道你想支持的每種格式的頭文件的樣子。