在 C# 中读取扩展图像属性
如果可能的话(出于性能原因),我想在不打开图像的情况下找到磁盘上图像的高度/宽度。
Windows 图像属性窗格包含宽度、高度、位深度等信息,这让我相信它在文件的某个位置存储元数据。 我如何访问这些信息?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
stackoverflow 有一些关于如何从图像中读取 EXIF 信息的问题,例如: 如何使用C#从文件中获取EXIF数据
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#
使用 System.Drawing.Image 类。
Use System.Drawing.Image class.
实现此目的的最简单方法是,假设图像是正方形,则获取文件大小(以字节为单位)并取平方根。 这将是您的宽度和高度。
:-)
或者,您可以尝试使用此 codeplex 库
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.
:-)
Or, you can try reading the image's EXIF information using this codeplex library
Windows 不会将(此)元数据存储在文件系统中的任何特殊位置; 属性窗口只是从图像文件本身读取它们。
我认为 .NET 没有提供任何功能来仅从图像中读取元数据而不加载整个图像。 如果您只处理有限数量的不同图像格式(例如,仅 JPEG、PNG 和 GIF),那么您自己从图像标题中读取大小应该不会太难。
另一方面,如果您必须处理多种图像格式,也许您可以查看 Unix
文件
实用程序。 它能够检测许多不同图像格式的大小,并且启动速度非常快。Windows doesn't store (this) metadata in any special place in the filesystem; the Properties window simply reads them from the image file itself.
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.
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.为了获取图像的宽度和高度(本质上,正如您所说的,元数据),您必须打开文件,解析某种标头信息并以这种方式获得您想要的内容。
您不必读取所有颜色/位图信息,只需读取标题。
这与 Windows 能够从应用程序文件加载图标而不实际执行它们的方式相同。
In order to get the width and height of an image (essentially, as you put it, the metadata) you will have to open 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.
检查这个问题:
Check this question:
要读取 Windows 资源管理器显示的属性,您可以使用Microsoft Shell Controls and Automation组件。 这样做的优点是您不需要任何第三方库(COM 对象已经存在)或额外的代码来解析图像头,并且它可以处理多种格式。
示例代码可以在相关问题的答案中找到。
To read the properties displayed by Windows Explorer you can use the Microsoft Shell Controls and Automation component. 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.
Sample code can be found in an answer to a related question.