Read extended image properties in c#

别说谁变了你拦得住时间么 提交于 2019-12-12 09:37:00

问题


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?


回答1:


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#




回答2:


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



回答3:


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




回答4:


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.




回答5:


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.




回答6:


Check this question:

  • How do I reliably get an image dimensions in .NET without loading the image?



回答7:


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.



来源:https://stackoverflow.com/questions/1195778/read-extended-image-properties-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!