Click here to Skip to main content
15,885,137 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
How to get the system dpi settings in c# console application. Whether the dpi is 90 or 120 or some other custom setting.
Posted
Updated 13-Jun-19 3:38am

You can try this:

C#
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
    float dpiX = graphics.DpiX;
    float dpiY = graphics.DpiY;
}


NB: You will need a reference to the System.Drawing assembly.
 
Share this answer
 
v2
Comments
Alan N 7-Mar-13 7:31am    
Thats much neater than my way.
johannesnestler 7-Mar-13 7:41am    
Didn't OP say "CONSOLE APPLICATION"? - [Edit] ah sorry you mentioned reference to System.Drawing
Kuan Bartel 18-Sep-14 4:50am    
This doesn't work. On a system using 120 dpi then this returns 96.
Try this:

C#
using System;
using System.Runtime.InteropServices;

namespace DPI
{
    class Program
    {
        [DllImport("gdi32.dll")]
        static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr hWnd);

        /// <summary>
        /// Logical pixels inch in X
        /// </summary>
        const int LOGPIXELSX = 88;
        /// <summary>
        /// Logical pixels inch in Y
        /// </summary>
        const int LOGPIXELSY = 90;

        static void Main(string[] args)
        {
            IntPtr hdc = GetDC(IntPtr.Zero);

            Console.WriteLine(GetDeviceCaps(hdc, LOGPIXELSX));
            // or
            Console.WriteLine(GetDeviceCaps(hdc, LOGPIXELSY));
            Console.ReadKey();
        }
    }
}


No additional references needed. But if it's better to import gdi32 and user32 dll? I dont know - markovl's and Alan N's solutions look more elegant...
 
Share this answer
 
v3
The value is stored in the registry

For XP it's at
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontDPI LogPixels

You could create a Graphics object from the console window handle:
C#
using (Graphics g = Graphics.FromHwnd(Process.GetCurrentProcess().MainWindowHandle)) {
  Console.WriteLine("X {0:F0}dpi, Y {1:F0}dpi", g.DpiX, g.DpiY);
}

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\WindowsNT\CurrentVersion\FontDPI")) {
  Console.WriteLine(key.GetValue("LogPixels"));
}

Alan.
 
Share this answer
 
A little adaptation

C#
using System;
using System.Drawing;

namespace Helpers
{
    public class DPI
    {
        
        public static float[] GetDpi()
        {
            using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
            {
                float dpiX = graphics.DpiX;
                float dpiY = graphics.DpiY;
                float[] result = new float[] { dpiX = dpiX, dpiY = dpiY };
                return result;
            }
        } 

        
    }
}


using:

C#
float[] Dpi = DPI.GetDpi();
Console.WriteLine(Dpi[0]);


Output: 96
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900