Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read extended image properties in c#

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?

like image 209
Gabe Moothart Avatar asked Jul 28 '09 18:07

Gabe Moothart


3 Answers

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"));
like image 117
Md Shahzad Adil Avatar answered Sep 21 '22 09:09

Md Shahzad Adil


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#

like image 32
Zed Avatar answered Sep 22 '22 09:09

Zed


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

like image 35
Chris Ballance Avatar answered Sep 22 '22 09:09

Chris Ballance