Tutorial :Read extended image properties in c#



Question:

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?


Solution: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#


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


Solution: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


Solution: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.


Solution: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.


Solution:6

Check this question:


Solution: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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »