DPI图形屏幕分辨率像素WinForm PrintPageEventArgs [英] DPI Graphics Screen Resolution Pixels WinForm PrintPageEventArgs
问题描述
在我的应用程序正在运行的任何显示器上,Dpi点与像素如何相关?
How do Dpi Points relate to Pixels for any display my application is running on?
int points;
Screen primary;
public Form1() {
InitializeComponent();
points = -1;
primary = null;
}
void OnPaint(object sender, PaintEventArgs e) {
if (points < 0) {
points = (int)(e.Graphics.DpiX / 72.0F); // There are 72 points per inch
}
if (primary == null) {
primary = Screen.PrimaryScreen;
Console.WriteLine(primary.WorkingArea.Height);
Console.WriteLine(primary.WorkingArea.Width);
Console.WriteLine(primary.BitsPerPixel);
}
}
我现在是否拥有所需的所有信息?
Do I now have all of the information I need?
我可以使用以上任何信息来找出1200像素有多长吗?
Can I use any of the information above to find out just how long 1200 pixels is?
推荐答案
我知道已经有几个月了,但是在阅读有关WPF的书时,我遇到了答案:
I realize it has been a few months, but while reading a book on WPF, I came across the answer:
如果使用标准Windows DPI设置(96 dpi),每个与设备无关的单位都对应一个真实的物理像素。
If using standard Windows DPI setting (96 dpi), each device-independent unit corresponds to one real, physical pixel.
[Physical Unit Size] = [Device-Independent Unit Size] x [System DPI]
= 1/96 inch x 96 dpi
= 1 pixel
因此,通过Windows系统DPI设置可以达到96像素,达到一英寸。
Hence, 96 pixels to make an inch through the Windows system DPI settings.
但是,实际上,这取决于您的显示尺寸。
However, this does, in reality, depend on your display size.
对于设置为1600 x 1200分辨率的19英寸LDC显示器,请使用毕达哥拉斯定理可帮助计算显示器的像素密度:
For a 19-inch LDC monitor set to a resolution of 1600 x 1200, use the Pythagoras theorem helps to calculate pixel density for the monitor:
[Screen DPI] = Math.Sqrt(Math.Pow(1600, 2) + Math.Pow(1200, 2)) / 19
我使用此数据编写了一个静态工具,现在将其保存在所有项目的Tools类中:
Using this data, I wrote up a little static tool that I now keep in my Tools class of all my projects:
/// <summary>
/// Calculates the Screen Dots Per Inch of a Display Monitor
/// </summary>
/// <param name="monitorSize">Size, in inches</param>
/// <param name="resolutionWidth">width resolution, in pixels</param>
/// <param name="resolutionHeight">height resolution, in pixels</param>
/// <returns>double presision value indicating the Screen Dots Per Inch</returns>
public static double ScreenDPI(int monitorSize, int resolutionWidth, int resolutionHeight) {
//int resolutionWidth = 1600;
//int resolutionHeight = 1200;
//int monitorSize = 19;
if (0 < monitorSize) {
double screenDpi = Math.Sqrt(Math.Pow(resolutionWidth, 2) + Math.Pow(resolutionHeight, 2)) / monitorSize;
return screenDpi;
}
return 0;
}
我希望其他人可以从这个漂亮的小工具中受益。
I hope others get some use out of this nifty little tool.
这篇关于DPI图形屏幕分辨率像素WinForm PrintPageEventArgs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!