How to get DPI in C# .NET?

Learn Programming picture Learn Programming · Jul 27, 2011 · Viewed 35.4k times · Source

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.

Answer

Thorsten Dittmar picture Thorsten Dittmar · Jul 27, 2011

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();
}