Example #1
0
 public static void GetDpi(this Screen screen, DpiType dpiType, out uint dpiX, out uint dpiY)
 {
     try
     {
         var pnt = new System.Drawing.Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
         var mon = MonitorFromPoint(pnt, 2);
         GetDpiForMonitor(mon, dpiType, out dpiX, out dpiY);
     }
     catch (DllNotFoundException ex)
     {
         Logger.Log(ex.Message);
         dpiX = 96;
         dpiY = 96;
     }
 }
            /// <summary>
            /// Returns the scaling of the given screen.
            /// </summary>
            /// <param name="dpiType">The type of dpi that should be given back..</param>
            /// <param name="dpiX">Gives the horizontal scaling back (in dpi).</param>
            /// <param name="dpiY">Gives the vertical scaling back (in dpi).</param>
            private static void GetDpi(int x, int y, DpiType dpiType, out uint dpiX, out uint dpiY)
            {
                var point    = new System.Drawing.Point(x, y);
                var hmonitor = MonitorFromPoint(point, _MONITOR_DEFAULTTONEAREST);

                switch (GetDpiForMonitor(hmonitor, dpiType, out dpiX, out dpiY).ToInt32())
                {
                case _S_OK: return;

                case _E_INVALIDARG:
                    throw new ArgumentException("Unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information.");

                default:
                    throw new COMException("Unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information.");
                }
            }
Example #3
0
        /// <summary>
        /// Get DPI value from pointer
        /// </summary>
        /// <param name="point"></param>
        /// <param name="dpiType"></param>
        /// <param name="dpiX"></param>
        /// <param name="dpiY"></param>
        internal static void GetDpi(Point point, DpiType dpiType, out uint dpiX, out uint dpiY)
        {
            var mon = NativeMethods.MonitorFromPoint(point, 2 /*MONITOR_DEFAULTTONEAREST*/);

            if (IsWindows7())
            {
                Graphics g       = Graphics.FromHwnd(IntPtr.Zero);
                IntPtr   desktop = g.GetHdc();

                dpiX = NativeMethods.GetDeviceCaps(desktop, (int)DeviceCap.LOGPIXELSX);
                dpiY = NativeMethods.GetDeviceCaps(desktop, (int)DeviceCap.LOGPIXELSY);
            }
            else
            {
                NativeMethods.GetDpiForMonitor(mon, dpiType, out dpiX, out dpiY);
            }
        }
Example #4
0
        /// <summary>
        /// Represents the different types of scaling.
        /// </summary>
        /// <seealso cref="https://msdn.microsoft.com/en-us/library/windows/desktop/dn280511.aspx"/>


        /// <summary>
        /// Returns the scaling of the given screen.
        ///
        /// http://dotnet-snippets.com/snippet/get-screen-scaling/4755
        /// </summary>
        /// <param name="screen">The screen which scaling should be given back.</param>
        /// <param name="dpiType">The type of dpi that should be given back..</param>
        /// <param name="dpiX">Gives the horizontal scaling back (in dpi).</param>
        /// <param name="dpiY">Gives the vertical scaling back (in dpi).</param>
        public static void GetDpi(Screen screen, DpiType dpiType, out uint dpiX, out uint dpiY)
        {
            var point    = new Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
            var hmonitor = MonitorFromPoint(point, MONITOR_DEFAULTTONEAREST);

            int result = GetDpiForMonitor(hmonitor, dpiType, out dpiX, out dpiY);

            switch (result)
            {
            case S_OK: return;

            case E_INVALIDARG:
                throw new ArgumentException("Unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information.");

            default:
                throw new COMException("Unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information.");
            }
        }
Example #5
0
        public static uint GetDpi(System.Drawing.Point point, DpiType dpiType)
        {
            var mon = MonitorFromPoint(point, 2 /*MONITOR_DEFAULTTONEAREST*/);

            try
            {
                uint dpiX, dpiY;
                GetDpiForMonitor(mon, dpiType, out dpiX, out dpiY);
                return(dpiX);
            }
            catch
            {
                // fallback for Windows 7 and older - not 100% reliable
                Graphics graphics = Graphics.FromHdc(mon);
                float    dpiXX    = graphics.DpiX;
                return(Convert.ToUInt32(dpiXX));
            }
        }
Example #6
0
        /// <summary>
        /// Get Dpi of a Screen
        /// </summary>
        /// <param name="screen"></param>
        /// <param name="dpiType"></param>
        /// <returns></returns>
        public static uint GetDpi(this IScreen screen, DpiType dpiType)
        {
            if (screen == null)
            {
                throw new ArgumentNullException(nameof(screen));
            }

            try
            {
                var mon = NativeMethods.MonitorFromPoint(screen.Bounds.Location, 2 /*MONITOR_DEFAULTTONEAREST*/);
                NativeMethods.GetDpiForMonitor(mon, dpiType, out var dpiX, out var dpiY);
                return(dpiX);
            }
            catch (DllNotFoundException)
            {
                // On Windows <8, just assume scaling 100%.
                return(DefaultDpi);
            }
        }
Example #7
0
        public static uint GetDpi(IntPtr hwnd, DpiType dpiType)
        {
            var screen = Screen.FromHandle(hwnd);
            var pnt    = new System.Drawing.Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
            var mon    = MonitorFromPoint(pnt, 2 /*MONITOR_DEFAULTTONEAREST*/);

            try
            {
                uint dpiX, dpiY;
                GetDpiForMonitor(mon, dpiType, out dpiX, out dpiY);
                return(dpiX);
            }
            catch
            {
                // fallback for Windows 7 and older - not 100% reliable
                Graphics graphics = Graphics.FromHwnd(hwnd);
                float    dpiXX    = graphics.DpiX;
                return(Convert.ToUInt32(dpiXX));
            }
        }
Example #8
0
        public static uint GetDpi(IntPtr hwnd, DpiType dpiType)
        {
            var screen      = Screen.FromHandle(hwnd);
            var screenPoint = new Point((int)screen.Bounds.Left, (int)screen.Bounds.Top);
            var monitor     = MonitorFromPoint(screenPoint, 2 /*MONITOR_DEFAULTTONEAREST*/);

            uint dpiX = 96;

            try
            {
                GetDpiForMonitor(monitor, dpiType, out dpiX, out _);
            }
            catch (Exception ex)
            {
                EventLogContext.TryWriteExceptionToLogBlocking(ex, "MainWindow.xaml.cs",
                                                               "Failed to Get Dpi from Shcore.dll private static extern IntPtr GetDpiForMonitor");
            }

            return(dpiX);
        }
Example #9
0
        public static Point GetMonitorDpi(Screen screen)
        {
            if (IsWindows81OrNewer())
            {
                var point    = new Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
                var hmonitor = MonitorFromPoint(point, MONITOR_DEFAULTTONEAREST);

                uint    dpiX, dpiY;
                DpiType type = DpiType.Effective; // see https://msdn.microsoft.com/en-us/library/windows/desktop/dn280511.aspx

                try
                {
                    switch (GetDpiForMonitor(hmonitor, type, out dpiX, out dpiY).ToInt32())
                    {
                    case S_OK:
                        return(new Point(Convert.ToInt32(dpiX), Convert.ToInt32(dpiX)));

                    case E_INVALIDARG:
                        Console.Out.WriteLine(
                            "Unable to fetch monitor DPI in Utiliy.cs, Invalid argument used to call Win32 function. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information.");
                        break;

                    default:
                        Console.Out.WriteLine(
                            "Unable to fetch monitor DPI in Utility.cs, unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information.");
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.Out.WriteLine(
                        "An exception occurred in Utility.cs, method GetMonitorDpi(Screen screen). Message: " +
                        ex.Message);
                }
            }
            // Fall back and general system-wide DPI for Windows 8 and earlier versions
            return(GetSystemDpi());
        }
Example #10
0
        public static Dpi GetDpi(this System.Windows.Forms.Screen screen, DpiType dpiType)
        {
            Dpi dpi = new Dpi();
            var pnt = new System.Drawing.Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
            var mon = MonitorFromPoint(pnt, 2 /*MONITOR_DEFAULTTONEAREST*/);

            Win32APIHelper.RtlGetVersion(out Win32APIHelper.OsVersionInfo osVersionInfo);
            if (osVersionInfo.MajorVersion != 10)
            {
                Matrix m =
                    PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice;
                dpi.x = (uint)m.M11 * 96; // notice it's divided by 96 already
                dpi.y = (uint)m.M22 * 96; // notice it's divided by 96 already
            }
            else
            {
                uint dpiX, dpiY;
                GetDpiForMonitor(mon, dpiType, out dpiX, out dpiY);
                dpi.x = dpiX;
                dpi.y = dpiY;
            }
            return(dpi);
        }
 internal static extern uint GetDpiForMonitor([In] IntPtr hmonitor, [In] DpiType dpiType, [Out] out uint dpiX, [Out] out uint dpiY);
Example #12
0
 private static extern IntPtr GetDpiForMonitor([In] IntPtr hmonitor, [In] DpiType dpiType, [Out] out uint dpiX, [Out] out uint dpiY);
Example #13
0
 public static extern int GetDpiForMonitor(IntPtr hmon, DpiType dpiType, out uint dpiX, out uint dpiY);
Example #14
0
 public static extern bool GetDpiForMonitor([In] IntPtr hMonitor, [In] DpiType dpiType, [Out] out uint dpiX, [Out] out uint dpiY);
Example #15
0
 internal static extern HRESULT GetDpiForMonitor(
     IntPtr hMonitor,
     DpiType dpiType,
     out uint dpiX,
     out uint dpiY);
Example #16
0
        public static uint GetDpi(Window window, DpiType dpiType)
        {
            var hwnd = new WindowInteropHelper(window).Handle;

            return(GetDpi(hwnd, dpiType));
        }
 public static void GetDpi(this System.Windows.Forms.Screen screen, DpiType dpiType, out uint dpiX, out uint dpiY)
 {
     var pnt = new System.Drawing.Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
       var mon = MonitorFromPoint(pnt, 2/*MONITOR_DEFAULTTONEAREST*/);
       GetDpiForMonitor(mon, dpiType, out dpiX, out dpiY);
 }
Example #18
0
 [DllImport("Shcore.dll")] private static extern IntPtr GetDpiForMonitor([In] IntPtr hmonitor, [In] DpiType dpiType, [Out] out uint dpiX, [Out] out uint dpiY);