Skip to content

Instantly share code, notes, and snippets.

@nitinjs
Created September 25, 2018 06:39
Show Gist options
  • Save nitinjs/27505e1fba5ab85fb2fe94cb51886999 to your computer and use it in GitHub Desktop.
Save nitinjs/27505e1fba5ab85fb2fe94cb51886999 to your computer and use it in GitHub Desktop.
Allows to get device ppi in C#
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment