WinForm DPI Screen Resolution Graphic Pixels PrintPageEventArgs

How do dpi points relate to pixels for any display my app 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);
  }
}

Now do I have all the information I need?

Can I use any of the above to find out how long 1200 pixels are?

+3
source share
3 answers

I understand that several months have passed, but reading a book about WPF, I came across an answer:

Using the standard Windows DPI setting (96 dpi), each device-independent block corresponds to one real physical pixel.

[Physical Unit Size] = [Device-Independent Unit Size] x [System DPI]
                     = 1/96 inch x 96 dpi
                     = 1 pixel

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

However, in reality it depends on your screen size.

19- LDC, 1600 x 1200, , :

[Screen DPI] = Math.Sqrt(Math.Pow(1600, 2) + Math.Pow(1200, 2)) / 19

, , "" :

/// <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;
}

, .

+1

DPI "Dots Per Inch" - = . , , 1200 :

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

: pixels = Graphics.DpiY * points/72

, , 1 '0,010 . 96 , , .

Creating screenshots of your form and printing them is a bad idea. Printers have a much higher resolution, a typical resolution of 600 dpi. The printout will look grainy, as each pixel on the screen becomes a 6x6 block on paper. Especially noticeable and muted for text with anti-aliasing.

+1
source

Source: https://habr.com/ru/post/1794805/


All Articles