Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 确定图像文件的格式?_C#_Image_Image Processing - Fatal编程技术网

C# 确定图像文件的格式?

C# 确定图像文件的格式?,c#,image,image-processing,C#,Image,Image Processing,如何通过编程确定图像文件的图像格式,包括特定的编码,如TIFF组?如果您没有找到任何现成的库,我想您应该以二进制形式打开文件并查找标题数据,这意味着您必须知道您想要支持的每种格式的标题的外观。请参见我的答案: 请看这篇文章:它解释了最常见的图像文件的标题,您必须阅读它,然后将其与您期望的匹配,或者简单地从标题推断图像格式。这是一篇好文章,但它对格式特定的属性没有帮助。@Ross@Reno我正在看。IText可能会更简单,谢谢。您可以在这里看到一个非常简单的解决方案:[1]一个恼人的副作用是,如

如何通过编程确定图像文件的图像格式,包括特定的编码,如TIFF组?

如果您没有找到任何现成的库,我想您应该以二进制形式打开文件并查找标题数据,这意味着您必须知道您想要支持的每种格式的标题的外观。

请参见我的答案:


请看这篇文章:它解释了最常见的图像文件的标题,您必须阅读它,然后将其与您期望的匹配,或者简单地从标题推断图像格式。这是一篇好文章,但它对格式特定的属性没有帮助。@Ross@Reno我正在看。IText可能会更简单,谢谢。您可以在这里看到一个非常简单的解决方案:[1]一个恼人的副作用是,如果文件不是图像,那么尝试从流中加载它们会悄悄抛出一个奇怪的OutOfMemory异常。至少在VisualStudio中调试时可以看到报告。看到所有这些OOM异常是透明的,但令人恼火。
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");