Reputation: 6500
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.
Upvotes: 1
Views: 8278
Reputation: 18997
This might help , using System.Drawing namspace
System.Drawing.Image objImage = System.Drawing.Image.FromFile(Filepath);
imageWidth = objImage.Width;
imageHeight = objImage.Height;
Upvotes: 1
Reputation: 7249
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.
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
Reading Image Headers to Get Width and Height will work for JPG, GIF, PNG and BMP image types.
Upvotes: 1