如何获得图像的分辨率? (JPEG,GIF,PNG,JPG) [英] How can I get the resolution of an image? (JPEG, GIF, PNG, JPG)

查看:156
本文介绍了如何获得图像的分辨率? (JPEG,GIF,PNG,JPG)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个方法:

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

但我真的不明白如何获取我正在寻找的东西。我对DPI不感兴趣,我只需要1024x768,1200x1024等号码。另外,每次我想要找到图像的分辨率时,我是否必须创建一个新的图像对象?

But I don't really understand how to fetch what I'm looking for. I'm not interested in DPI, I just need the 1024x768, 1200x1024 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. :)

推荐答案

1024x768在计算机显示设置的上下文中仅称为分辨率,即便如此一个不幸的用词不当。成像1024x768只是 width &图像的高度,正如其他人所提到的,您已经显示了检索这些数字的代码。略微调整:

"1024x768" is only called a "resolution" in the context of a computer's display settings, and even then it's an unfortunate misnomer. In imaging "1024x768" 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:

var img = Image.FromFile(@"C:\image.jpg");
Console.WriteLine(img.Width + "x" + img.Height); // prints "1024x768" (etc)

获取数字的唯一内置方法之后是创建一个新实例(事实上,解码整个图像) - 如果你只需要为几百个图像检索这些数字,这将是非常低效的。避免这种情况很难;这是一个起点:如何在不加载图像的情况下可靠地获取.NET中的图像尺寸?

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?

当你说话时分辨率通常指的是点数或每英寸像素数位图类将此存储在 Horizo​​ntalResolution / VerticalResolution 属性中,而 DpiX中的Graphics类 / DpiY

When you speak of "resolution" you normally refer to the number of dots, or pixels, per inch. The Bitmap class stores this in the HorizontalResolution / VerticalResolution properties, while the Graphics class in DpiX / DpiY.

此计算:

bmp.Width * (g.DpiX / bmp.HorizontalResolution)

可以重新排列,如下所示:

can be rearranged for clarity as follows:

(bmp.Width / bmp.HorizontalResolution) * g.DpiX

括号中的部分计算图像的宽度,以英寸为单位。乘以 g.DpiX ,然后给出 Graphics 对象中具有相同宽度(以英寸为单位)的像素数图像 bmp

where the bracketed part computes the width of the image in inches. Multiplying by g.DpiX then gives you the number of pixels in the Graphics object that have the same width in inches as the image bmp.

这篇关于如何获得图像的分辨率? (JPEG,GIF,PNG,JPG)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆