在 C# 中读取扩展图像属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1195778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read extended image properties in c#
提问by Gabe Moothart
I would like to find the height/width of an image on disk without opening it, if possible (for performance reasons).
如果可能(出于性能原因),我想在不打开磁盘的情况下找到磁盘上图像的高度/宽度。
The Windows properties pane for images contains information like width, height, bit depth, etc., which leads me to believe that it is storing metadata on the file somewhere. How can I access this information?
图像的 Windows 属性窗格包含诸如宽度、高度、位深度等信息,这让我相信它正在将元数据存储在文件中的某处。我怎样才能访问这些信息?
采纳答案by Zed
There are some stackoverflow questions on how to read the EXIF information from images, such as: How to get the EXIF data from a file using C#
有一些关于如何从图像中读取 EXIF 信息的 stackoverflow 问题,例如:How to get the EXIF data from a file using C#
回答by Chris Ballance
The easiest way to accomplish this is, assuming the image is square is to take the file size in bytes and take the square root. This will be your width and height.
完成此操作的最简单方法是,假设图像是方形的,则取文件大小(以字节为单位)并取平方根。这将是您的宽度和高度。
256 bytes = 16px x 16px
Or, you can try reading the image's EXIF information using this codeplex library
或者,您可以尝试使用此codeplex 库读取图像的 EXIF 信息
回答by Thomas
Windows doesn't store (this) metadata in any special place in the filesystem; the Properties window simply reads them from the image file itself.
Windows 不会在文件系统中的任何特殊位置存储(这个)元数据;属性窗口只是从图像文件本身读取它们。
I don't think .NET offers any functions to read just the metadata from an image without loading the entire image. If you're dealing with only a limited number of different image formats (e.g. only JPEG, PNG and GIF), it shouldn't be too hard to read the size from the image header yourself.
我不认为 .NET 提供任何功能来从图像中读取元数据而不加载整个图像。如果您只处理有限数量的不同图像格式(例如,只有 JPEG、PNG 和 GIF),那么自己从图像标题中读取大小应该不会太难。
If, on the other hand, you have to deal with many image formats, maybe you can have a look at the source code of the Unix file
utility. It manages to detect the size of many, many different image formats, and is blazingly fast to boot.
另一方面,如果您必须处理许多图像格式,也许您可以查看Unixfile
实用程序的源代码。它设法检测许多不同图像格式的大小,并且启动速度非常快。
回答by Mike J
In order to get the width and height of an image (essentially, as you put it, the metadata) you will have toopen the file, parse some kind of header information and obtain what you want that way.
为了获得图像的宽度和高度(本质上,正如您所说的,元数据)您必须打开文件,解析某种标题信息并以这种方式获取您想要的内容。
You would not have to read all the color/bitmap information, only the header.
您不必阅读所有颜色/位图信息,只需阅读标题。
This is the same way Windows is able to load icons from application files without actually executing them.
这与 Windows 能够从应用程序文件加载图标而不实际执行它们的方式相同。
回答by CMS
Check this question:
检查这个问题:
回答by Dirk Vollmar
To read the properties displayed by Windows Explorer you can use the Microsoft Shell Controls and Automationcomponent. The advantage of this is that you don't need any third party library (the COM object is already there) or additional code for parsing the image header and that it will work with a variety of formats.
要读取 Windows 资源管理器显示的属性,您可以使用Microsoft Shell 控件和自动化组件。这样做的好处是你不需要任何第三方库(COM 对象已经存在)或额外的代码来解析图像头,并且它可以处理各种格式。
Sample code can be found in an answer to a related question.
示例代码可以在相关问题的回答中找到。
回答by Md Shahzad Adil
Use System.Drawing.Image class.
使用 System.Drawing.Image 类。
Image img = Image.FromFile(fileName);
ImageFormat format = img.RawFormat;
Console.WriteLine("Image Type : "+format.ToString());
Console.WriteLine("Image width : "+img.Width);
Console.WriteLine("Image height : "+img.Height);
Console.WriteLine("Image resolution : "+(img.VerticalResolution*img.HorizontalResolution));
Console.WriteLine("Image Pixel depth : "+Image.GetPixelFormatSize(img.PixelFormat));
Console.WriteLine("Image Creation Date : "+creation.ToString("yyyy-MM-dd"));
Console.WriteLine("Image Creation Time : "+creation.ToString("hh:mm:ss"));
Console.WriteLine("Image Modification Date : "+modify.ToString("yyyy-MM-dd"));
Console.WriteLine("Image Modification Time : "+modify.ToString("hh:mm:ss"));