获取图像宽度和高度而不创建位图对象

时间:2012-06-23 09:58:24

标签: c# .net image gdi+

我想要获得图像的高度和宽度。目前正在做的是

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

有没有更好的方法来创建和处理Bitmap对象,从而不浪费内存。

2 个答案:

答案 0 :(得分:1)

从下面的引用看来,在.NET 2.0类库中没有它的功能。代码块下面应该可以工作,

目前没有错误检查或任何其他验证,根据EXIF数据的数量,它通常不会从图像读取6K以上。

示例代码

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

    }
}

参考:Getting image dimensions without reading the entire file

更新

Reading Image Headers to Get Width and Height适用于JPG,GIF,PNG和BMP图片类型。

答案 1 :(得分:1)

这可能会有所帮助,使用System.Drawing命名空间

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