DPI图形屏幕分辨率像素WinForm PrintPageEventArgs

jp2*_*ode 5 c# printing graphics dpi winforms

对于我的应用程序正在运行的任何显示,Dpi点与像素如何相关?

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

我现在有我需要的所有信息吗?

我可以使用以上任何信息来找出1200像素有多长吗?

小智 2

我意识到已经过去几个月了,但是在阅读一本有关 WPF 的书时,我发现了答案:

如果使用标准 Windows DPI 设置 (96 dpi),则每个与设备无关的单位对应于一个真实的物理像素。

[Physical Unit Size] = [Device-Independent Unit Size] x [System DPI]
                     = 1/96 inch x 96 dpi
                     = 1 pixel
Run Code Online (Sandbox Code Playgroud)

因此,通过Windows系统DPI设置,96像素为一英寸。

然而,实际上,这确实取决于您的显示器尺寸。

对于分辨率设置为 1600 x 1200 的 19 英寸 LDC 显示器,使用勾股定理有助于计算显示器的像素密度:

[Screen DPI] = Math.Sqrt(Math.Pow(1600, 2) + Math.Pow(1200, 2)) / 19
Run Code Online (Sandbox Code Playgroud)

使用这些数据,我编写了一个小静态工具,现在将其保存在所有项目的 Tools 类中:

/// <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;
}
Run Code Online (Sandbox Code Playgroud)

我希望其他人能从这个漂亮的小工具中得到一些用处。