Skip to content

Instantly share code, notes, and snippets.

@relliv
Created February 22, 2019 20:20
Show Gist options
  • Save relliv/d1114411d1579e2d42f7a6836c27c6cc to your computer and use it in GitHub Desktop.
Save relliv/d1114411d1579e2d42f7a6836c27c6cc to your computer and use it in GitHub Desktop.
C# Get Monitor Size As Inch
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
private void Button_Click(object sender, RoutedEventArgs e)
{
Graphics graphics = Graphics.FromHwnd(IntPtr.Zero); // using System.Drawing; (add from referances)
IntPtr desktop = graphics.GetHdc();
int monitorHeight = GetDeviceCaps(desktop, 6);
int monitorWidth = GetDeviceCaps(desktop, 4);
Content = $"Height: {monitorHeight} mm., Width: {monitorWidth} mm. This Monitor Size: {Math.Sqrt(Math.Pow(monitorHeight, 2) + Math.Pow(monitorWidth, 2)) / 25,4:#,##0.00} inch.";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment