14

I'm trying to build a Windows Forms application using C#.

How do I get the DPI in .NET?

I've read before that there is DPIX and DPIY, which can be used in .NET to get the current DPI.

Is that correct?

Thanks all.

aaron
  • 39,695
  • 6
  • 46
  • 102
Learn Programming
  • 141
  • 1
  • 1
  • 3

2 Answers2

33

Use an instance of the Graphics class. You get this using the following within your form (could be in form's Load event handler):

float dx, dy;

Graphics g = this.CreateGraphics();
try
{
    dx = g.DpiX;
    dy = g.DpiY;
}
finally
{
    g.Dispose();
}
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • if convert pixel to point use DPI value, which DPI (DpiX or DpiY) that can use? Or there are some tricks to convert a pixel value into points ? Thanks ... – Learn Programming Jul 29 '11 at 06:37
  • Well, a pixel is a location, so it has an X and an Y coordinate. Use DpiX to find the X coordinate and DpiY to find the Y coordinate. The same thing applies to scaling stuff - the horizontal scale factor is determined using DpiX, the vertical scale factor using DpiY. – Thorsten Dittmar Jul 29 '11 at 07:40
  • Good answer, better than the (found elswhere) overloading of the `scaleControl` method (or sth. like that). BTW, if you want the "scale relative to default DPI", you only need to divide this by `96.0f`. – Tomasz Gandor Jul 24 '14 at 13:07
  • 9
    I'm finding that it returns 96 even on Windows 10 with scaling set to 150%. This is in an older application that doesn't tell windows that it is DPI aware. I think there must be an extra change needed for this method to work. – Daniel Earwicker Jul 13 '16 at 10:18
  • 3
    @DanielEarwicker Dpi doesn't depend on windows' scaling. It depends on the screen. The dpi value indicates how many pixels there are in a inch (or ppi - pixels per inch). It has different X and Y values because the pixels aren't perfect squares, they're actually rectangular. You use DPI when you want to display something in real world size (for instance, if you want to display a rectangle that is 3 inches wide and 5 inches tall on the screen), its size in pixels will be `Width = 3*DpiX; Height = 5*DpiY;` – Matheus Rocha Apr 14 '18 at 12:15
  • For the same result, one can just read `AutoScaleDimensions` of the form. It will also return (96, 96) if the Windows scaling is set higher but the app is not declared DPI-aware. In which case it's fair if you need this info to lay out/scale content: Windows does it for you. If you set "Program performs scaling" in the app compatibility settings, you'll get 144 or whatever. – Zeus Jul 08 '21 at 08:00
6

Modern Way to Get the DPI in a WinForms Application

In .NET Framework 4.7 or newer (for example .NET 6.0), you can use the Control.DeviceDpi property to get the DPI value for the screen on which the control is currently being displayed.

int dpi = this.DeviceDpi;

The value returned by the Control.DeviceDpi property depends on the DPI awareness mode of the application. In .NET 5.0 and newer, you can set the DPI awareness of a WinForms application in its entry point using the Application.SetHighDpiMode method. Possible modes are listed on this MSDN page.

Pavel Vladov
  • 4,707
  • 3
  • 35
  • 39