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.

3
  • Take a look here Commented Jun 23, 2012 at 10:02
  • @techno Did my answer helped you or you found your own solution then please let me know ? Commented Sep 7, 2012 at 7: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. Commented Sep 13, 2014 at 12:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@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.
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.
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 Article here mentions that it supports JPG, GIF, PNG and BMP image types. codeproject.com/Articles/35978/…
1

This might help , using System.Drawing namspace

 System.Drawing.Image objImage = System.Drawing.Image.FromFile(Filepath);
            imageWidth = objImage.Width;
            imageHeight = objImage.Height; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.