使用 C# 和 .NET 环境获取 jpeg 图像的分辨率?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/123838/
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
Get the resolution of a jpeg image using C# and the .NET Environment?
提问by Josh Mein
Our clients will be uploading images to be printed on their documents and we have been asked to come up with a way to get the resolution of the image in order to warn them if the image has too low of a resolution and will look pixalated in the end-product
我们的客户将上传要打印在他们的文档上的图像,我们被要求想出一种方法来获得图像的分辨率,以便在图像分辨率过低并且在图像中看起来像像素化时警告他们成品
If it comes to it we could also go with the dimensions if anyone knows how to get those but the resolution would be preferred
如果涉及到它,我们也可以使用尺寸,如果有人知道如何获得这些尺寸,但分辨率将是首选
Thank you
谢谢
回答by Xian
Image newImage = Image.FromFile("SampImag.jpg");
newImage.HorizontalResolution
回答by Brian ONeil
It depends what you are looking for... if you want the DPI of the image then you are looking for the HorizontalResolution which is the DPI of the image.
这取决于您在寻找什么...如果您想要图像的 DPI,那么您正在寻找 HorizontalResolution,它是图像的 DPI。
Image i = Image.FromFile(@"fileName.jpg");
i.HorizontalResolution;
If you want to figure out how large the image is then you need to calculate the measurements of the image which is:
如果您想弄清楚图像有多大,那么您需要计算图像的测量值,即:
int docHeight = (i.Height / i.VerticalResolution);
int docWidth = (i.Width / i.HorizontalResolution);
This will give you the document height and width in inches which you could then compare to the min size needed.
这将为您提供以英寸为单位的文档高度和宽度,然后您可以将其与所需的最小尺寸进行比较。
回答by Gabriel Mailhot
DPI take sense when printing only. 72dpi is the Mac standard and 96dpi is the Windows standard. Screen resolution only takes pixels into account, so a 72dpi 800x600 jpeg is the same screen resolution than a 96dpi 800x600 pixels.
DPI 仅在打印时才有意义。72dpi 是 Mac 标准,96dpi 是 Windows 标准。屏幕分辨率仅考虑像素,因此 72dpi 800x600 jpeg 与 96dpi 800x600 像素的屏幕分辨率相同。
Back to the '80s, Mac used 72dpi screen/print resolution to fit the screen/print size, so when you had an image on screen at 1:1, it correspond to the same size on the printer. Windows increased the screen resolution to 96dpi to have better font display.. but as a consequence, the screen image doesn't fit the printed size anymore.
回到 80 年代,Mac 使用 72dpi 屏幕/打印分辨率来适应屏幕/打印尺寸,因此当您在屏幕上以 1:1 显示图像时,它对应于打印机上的相同尺寸。Windows 将屏幕分辨率增加到 96dpi 以获得更好的字体显示。但结果,屏幕图像不再适合打印尺寸。
So, for web project, don't bother with DPI if the image isn't for print; 72dpi, 96dpi, even 1200dpi should display the same.
因此,对于 Web 项目,如果图像不是用于打印的,请不要理会 DPI;72dpi、96dpi,甚至 1200dpi 都应该显示相同。
回答by jlew
Image image = Image.FromFile( [file] );
GraphicsUnit unit = GraphicsUnit.Point;
RectangleF rect = image.GetBounds( ref unit );
float hres = image.HorizontalResolution;
float vres = image.VerticalResolution;

