Skip to content

Instantly share code, notes, and snippets.

@relliv
Created February 22, 2019 20:20
Show Gist options
  • Save relliv/3432ca6e64d60c1fbd13d4a7222ab3f8 to your computer and use it in GitHub Desktop.
Save relliv/3432ca6e64d60c1fbd13d4a7222ab3f8 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.";
}
@relliv
Copy link
Author

relliv commented Feb 22, 2019

Source

Edit: required System.Drawing reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment