From 689a3a78781724a48380e8b1fd7a20ae7cc02d51 Mon Sep 17 00:00:00 2001 From: Paddy Xu Date: Wed, 3 May 2017 20:12:57 +0300 Subject: [PATCH] Respect screen DPI when obtaining thumbnails --- QuickLook/Plugin/InfoPanel/DpiHelpers.cs | 46 ++++++++++++++++++++ QuickLook/Plugin/InfoPanel/InfoPanel.xaml | 2 +- QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs | 4 +- QuickLook/QuickLook.csproj | 1 + 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 QuickLook/Plugin/InfoPanel/DpiHelpers.cs diff --git a/QuickLook/Plugin/InfoPanel/DpiHelpers.cs b/QuickLook/Plugin/InfoPanel/DpiHelpers.cs new file mode 100644 index 0000000..c82256c --- /dev/null +++ b/QuickLook/Plugin/InfoPanel/DpiHelpers.cs @@ -0,0 +1,46 @@ +using System; +using System.Drawing; +using System.Runtime.InteropServices; + +namespace QuickLook.Plugin.InfoPanel +{ + internal static class DpiHelper + { + public enum DeviceCap + { + /// + /// Logical pixels inch in X + /// + LOGPIXELSX = 88, + /// + /// Logical pixels inch in Y + /// + LOGPIXELSY = 90 + } + + public const float DEFAULT_DPI = 96; + + public static Dpi GetCurrentDpi() + { + var g = Graphics.FromHwnd(IntPtr.Zero); + var desktop = g.GetHdc(); + + var dpi = new Dpi + { + HorizontalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSX), + VerticalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSY) + }; + + return dpi; + } + + [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] + public static extern int GetDeviceCaps(IntPtr hDC, int nIndex); + } + + internal class Dpi + { + public float HorizontalDpi { get; set; } + public float VerticalDpi { get; set; } + } +} \ No newline at end of file diff --git a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml index 398bec8..0823d68 100644 --- a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml +++ b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml @@ -10,7 +10,7 @@ - + diff --git a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs index 707a0db..c96dc97 100644 --- a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs +++ b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs @@ -26,7 +26,9 @@ namespace QuickLook.Plugin.InfoPanel public void DisplayInfo(string path) { var icon = - WindowsThumbnailProvider.GetThumbnail(path, 256, 256, + WindowsThumbnailProvider.GetThumbnail(path, + (int) (128 * DpiHelper.GetCurrentDpi().HorizontalDpi / DpiHelper.DEFAULT_DPI), + (int) (128 * DpiHelper.GetCurrentDpi().VerticalDpi / DpiHelper.DEFAULT_DPI), ThumbnailOptions.ScaleUp); image.Source = icon.ToBitmapSource(); diff --git a/QuickLook/QuickLook.csproj b/QuickLook/QuickLook.csproj index e2da01d..c9b7589 100644 --- a/QuickLook/QuickLook.csproj +++ b/QuickLook/QuickLook.csproj @@ -80,6 +80,7 @@ +