1

I want to get the Height and Width of an Image.Currently what im doing is

Bitmap b=new Bitmap(path);
w=b.width;h=b.height;

Is there a better way to do it without creating and disposing Bitmap objects and thus not wasting memory.

techno
  • 6,100
  • 16
  • 86
  • 192
  • Take a look [here](http://stackoverflow.com/questions/111345/getting-image-dimensions-without-reading-the-entire-file) – dvvrd Jun 23 '12 at 10:02
  • @techno Did my answer helped you or you found your own solution then please let me know ? – Harsh Baid Sep 07 '12 at 07:22
  • Without creating image or bitmap object you can obtain width and height through shell property handler (`PKEY_Image_HorizontalResolution` and `PKEY_Image_HorizontalResolution` properties). This works for many formats, however the problem is that this is native API, not managed. – Roman R. Sep 13 '14 at 12:13

2 Answers2

1

It seems from the reference below that in the .NET 2.0 class library there is no functionality for it. Below code block should work,

There is no error checking or any other verification currently, and it usually does not read more that 6K from an image depending on the amount of the EXIF data.

Sample Code

using System;
using System.IO;

namespace Test
{

    class Program
    {

        static bool GetJpegDimension(
            string fileName,
            out int width,
            out int height)
        {

            width = height = 0;
            bool found = false;
            bool eof = false;

            FileStream stream = new FileStream(
                fileName,
                FileMode.Open,
                FileAccess.Read);

            BinaryReader reader = new BinaryReader(stream);

            while (!found || eof)
            {

                // read 0xFF and the type
                reader.ReadByte();
                byte type = reader.ReadByte();

                // get length
                int len = 0;
                switch (type)
                {
                    // start and end of the image
                    case 0xD8: 
                    case 0xD9: 
                        len = 0;
                        break;

                    // restart interval
                    case 0xDD: 
                        len = 2;
                        break;

                    // the next two bytes is the length
                    default: 
                        int lenHi = reader.ReadByte();
                        int lenLo = reader.ReadByte();
                        len = (lenHi << 8 | lenLo) - 2;
                        break;
                }

                // EOF?
                if (type == 0xD9)
                    eof = true;

                // process the data
                if (len > 0)
                {

                    // read the data
                    byte[] data = reader.ReadBytes(len);

                    // this is what we are looking for
                    if (type == 0xC0)
                    {
                        width = data[1] << 8 | data[2];
                        height = data[3] << 8 | data[4];
                        found = true;
                    }

                }

            }

            reader.Close();
            stream.Close();

            return found;

        }

        static void Main(string[] args)
        {
            foreach (string file in Directory.GetFiles(args[0]))
            {
                int w, h;
                GetJpegDimension(file, out w, out h);
                System.Console.WriteLine(file + ": " + w + " x " + h);
            }
        }

    }
}

Reference: Getting image dimensions without reading the entire file

Update

Reading Image Headers to Get Width and Height will work for JPG, GIF, PNG and BMP image types.

Community
  • 1
  • 1
Harsh Baid
  • 7,199
  • 5
  • 48
  • 92
  • @yogi As mentioned in the reference link: It is indeed working for `.jpg` or `.jpeg` images but may be not work properly for some older formats of `.bmp` images. – Harsh Baid Jun 23 '12 at 10:13
  • 1
    You can't have a generic solution for all file types with this kind of problem. You'll have to know how to read each file type, and decide which action you take according to the image type. – SimpleVar Jun 23 '12 at 10:26
  • http://www.codeproject.com/Articles/35978/Reading-Image-Headers-to-Get-Width-and-Height – Harsh Baid Jun 23 '12 at 10:34
  • Of course the `Bitmap` approach is restricted to file types .NET can read and understand, to it's not a general solution for all possible image files either. – Joey Jun 23 '12 at 10:37
  • @joey Article here mentions that it supports JPG, GIF, PNG and BMP image types. http://www.codeproject.com/Articles/35978/Reading-Image-Headers-to-Get-Width-and-Height – Harsh Baid Jun 23 '12 at 12:15
1

This might help , using System.Drawing namspace

 System.Drawing.Image objImage = System.Drawing.Image.FromFile(Filepath);
            imageWidth = objImage.Width;
            imageHeight = objImage.Height; 
Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59