I found this method:
Graphics g = e.Graphics;
Bitmap bmp = new Bitmap("winter.jpg");
g.DrawImage(bmp, 0, 0);
Console.WriteLine("Screen resolution: " + g.DpiX + "DPI");
Console.WriteLine("Image resolution: " + bmp.HorizontalResolution + "DPI");
Console.WriteLine("Image Width: " + bmp.Width);
Console.WriteLine("Image Height: " + bmp.Height);
SizeF s = new SizeF(bmp.Width * (g.DpiX / bmp.HorizontalResolution),
bmp.Height * (g.DpiY / bmp.VerticalResolution));
Console.WriteLine("Display size of image: " + s);
But I don’t really understand how to fetch what I’m looking for. I’m not interested in DPI, I just need the 1024×768, 1200×1024 etc numbers. Also, would I have to create a new image object every time I want to find the resolution of the image?
I’m making an application that lists current images in a given folder, so any help would be appreciated. 🙂
“1024×768” is only called a “resolution” in the context of a computer’s display settings, and even then it’s an unfortunate misnomer. In imaging “1024×768” is simply the width & height of the image, so as others have mentioned, you’ve already shown the code that retrieves these numbers. Tweaked slightly:
The only built-in method to get the numbers you’re after is by creating a new instance (and, in fact, decoding the entire image) – which is going to be highly inefficient if you need to retrieve just these numbers for several hundred images. Avoiding this is hard; here’s a starting point: How do I reliably get an image dimensions in .NET without loading the image?
When you speak of “resolution” you normally refer to the number of dots, or pixels, per inch. The
Bitmapclass stores this in theHorizontalResolution/VerticalResolutionproperties, while the Graphics class inDpiX/DpiY.This calculation:
can be rearranged for clarity as follows:
where the bracketed part computes the width of the image in inches. Multiplying by
g.DpiXthen gives you the number of pixels in theGraphicsobject that have the same width in inches as the imagebmp.