开发者

DPI Graphics Screen Resolution Pixels WinForm PrintPageEventArgs

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.WorkingA开发者_如何学Gorea.Height);
    Console.WriteLine(primary.WorkingArea.Width);
    Console.WriteLine(primary.BitsPerPixel);
  }
}

Do I now have all of the information I need?

Can I use any of the information above to find out just how long 1200 pixels is?


DPI literally stands for "Dots Per Inch" - where dots==pixels. So to determine how long 1200 pixels is:

int inchesLong = (1200 / e.Graphics.DpiX);


I realize it has been a few months, but while reading a book on WPF, I came across the answer:

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

Hence, 96 pixels to make an inch through the Windows system DPI settings.

However, this does, in reality, depend on your display size.

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

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.


For the screen: pixels = Graphics.DpiY * points / 72

For the printer, mentioned in your question subject, mapping is 1 'pixel' == 0.010 inches by default. This is quite close to the default video dpi of 96 dots per inch making the copy on paper about the same size as what you see on the monitor.

Making screen shots of your form and printing them is a bad idea. Printers have much higher resolutions, 600 dpi is typical. The printout will look grainy as each pixel on the screen becomes a 6x6 blob on paper. Especially noticeable and fugly for anti-aliased text.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜