在 C# 中读取扩展图像属性

发布于 2024-07-30 07:43:19 字数 121 浏览 4 评论 0原文

如果可能的话(出于性能原因),我想在不打开图像的情况下找到磁盘上图像的高度/宽度。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

四叶草在未来唯美盛开 2024-08-06 07:43:19

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#

白鸥掠海 2024-08-06 07:43:19

使用 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"));

Use System.Drawing.Image class.

        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"));
弃爱 2024-08-06 07:43:19

实现此目的的最简单方法是,假设图像是正方形,则获取文件大小(以字节为单位)并取平方根。 这将是您的宽度和高度。

256 bytes = 16px x 16px

:-)

或者,您可以尝试使用此 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.

256 bytes = 16px x 16px

:-)

Or, you can try reading the image's EXIF information using this codeplex library

宛菡 2024-08-06 07:43:19

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.

心作怪 2024-08-06 07:43:19

为了获取图像的宽度和高度(本质上,正如您所说的,元数据),您必须打开文件,解析某种标头信息并以这种方式获得您想要的内容。

您不必读取所有颜色/位图信息,只需读取标题。

这与 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.

鱼忆七猫命九 2024-08-06 07:43:19

要读取 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文