C# 如何获得图像的分辨率?(JPEG、GIF、PNG、JPG)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2207187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:25:36  来源:igfitidea点击:

How can I get the resolution of an image? (JPEG, GIF, PNG, JPG)

c#.netimagec#-3.0

提问by

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 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?

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

I'm making an application that lists current images in a given folder, so any help would be appreciated. :)

我正在制作一个在给定文件夹中列出当前图像的应用程序,因此将不胜感激。:)

采纳答案by Roman Starkov

"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& heightof the image, so as others have mentioned, you've already shown the code that retrieves these numbers. Tweaked slightly:

“1024x768”仅在计算机显示设置的上下文中被称为“分辨率”,即使如此,它也是一个不幸的误称。在成像中,“1024x768”只是图像的宽度高度,因此正如其他人所提到的,您已经展示了检索这些数字的代码。稍微调整了一下:

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

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?

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



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

当您谈到“分辨率”时,您通常指的是每英寸点数或像素数Bitmap该类将其存储在HorizontalResolution/VerticalResolution属性中,而 Graphics 类存储在DpiX/ 中DpiY

This calculation:

这个计算:

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

can be rearranged for clarity as follows:

为了清楚起见,可以重新排列如下:

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

where the bracketed part computes the width of the image in inches. Multiplying by g.DpiXthen gives you the number of pixels in the Graphicsobject that have the same width in inches as the image bmp.

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

回答by Nick Craver

To get the size:

获取尺寸:

string path = @"C:\winter.jpg";
Image img = Image.FromFile(path);
Console.WriteLine("Image Width: " + img.Width); //equals img.Size.Width
Console.WriteLine("Image Height: " + img.Height); //equals img.Size.Height

Here's a full list of System.Drawing.Image properties, if case want to display others.

是 System.Drawing.Image 属性完整列表,如果需要显示其他属性

I'm not sure if there's a quicker way for an entire directory, with Windows Vista/7 there may be a newer API that contains this info, haven't come across it though.

我不确定整个目录是否有更快的方法,对于 Windows Vista/7,可能有一个包含此信息的较新 API,但还没有遇到它。

回答by Matt Warren

Surely you've answered your own question, it's as simple as this:

你肯定已经回答了你自己的问题,就这么简单:

Bitmap bmp = new Bitmap(@"winter.bmp");     
Console.WriteLine("Image Width: " + bmp.Width);
Console.WriteLine("Image Height: " + bmp.Height);

The DPI info is really only useful when you are displaying or printing the image. In the sample code it is converting the image to a Graphicsobject that can be used to draw it to the screen.

DPI 信息仅在您显示或打印图像时才有用。在示例代码中,它将图像转换为Graphics可用于将其绘制到屏幕的对象。

To answer the 2nd part of you question, you could parse the JPEG file header to find the width/height information without actually loading in the image. But to do this you'd need to know the header information and file format for JPEG files.

要回答问题的第二部分,您可以解析 JPEG 文件头以查找宽度/高度信息,而无需实际加载图像。但要做到这一点,您需要知道 JPEG 文件的标头信息和文件格式。