From 585d09bc10b959efbcdac0acf8d257ea89729db8 Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Sat, 16 Nov 2024 19:57:59 -0300 Subject: [PATCH] Update `GetBitmapFrameByWidthAndDpi` to round `dpi` frame value. --- CHANGELOG.md | 2 ++ Directory.Build.props | 2 +- ricaun.Revit.UI.Example/Revit/AppTheme.cs | 16 +++++++-------- ricaun.Revit.UI/BitmapExtension.cs | 24 +++++++++++++++++++---- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fee80a8..b31d3fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Update `GetBitmapFrame` to round `width` that is changed by `dpi`. - Add `GetBitmapFrameByWidthAndDpi` to get optimal frame by `dpi` and `width`. - Add `GetSystemDpi` to get the system `dpi` on the fly. +- Add `SystemDpi` to store the system `dpi` value. +- Update `GetBitmapFrameByWidthAndDpi` to round `dpi` frame value. ### Example - Add `Cube-Grey-Light.tiff` and `Cube-Grey-Dark.tiff` in `AppTheme`. diff --git a/Directory.Build.props b/Directory.Build.props index 3c1a252..55a80d0 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 0.7.1-alpha.1 + 0.7.1-alpha.2 \ No newline at end of file diff --git a/ricaun.Revit.UI.Example/Revit/AppTheme.cs b/ricaun.Revit.UI.Example/Revit/AppTheme.cs index d8aa158..7a4a9a1 100644 --- a/ricaun.Revit.UI.Example/Revit/AppTheme.cs +++ b/ricaun.Revit.UI.Example/Revit/AppTheme.cs @@ -65,14 +65,14 @@ public Result OnStartup(UIControlledApplication application) { var buttonTiff = ribbonPanel.CreatePushButton("Grey") .SetLargeImage("Resources/Cube-Grey-Light.tiff"); - //if (buttonTiff.LargeImage is System.Windows.Media.Imaging.BitmapSource largeImage) - //{ - // System.Console.WriteLine($"{largeImage.GetType().Name} | {largeImage.Width:0}x{largeImage.Height:0} ({largeImage.PixelWidth}x{largeImage.PixelHeight}) {largeImage.DpiX:0}:{largeImage.DpiY:0}"); - //} - //if (buttonTiff.Image is System.Windows.Media.Imaging.BitmapSource smallImage) - //{ - // System.Console.WriteLine($"{smallImage.GetType().Name} | {smallImage.Width:0}x{smallImage.Height:0} ({smallImage.PixelWidth}x{smallImage.PixelHeight}) {smallImage.DpiX:0}:{smallImage.DpiY:0}"); - //} + if (buttonTiff.LargeImage is System.Windows.Media.Imaging.BitmapSource largeImage) + { + System.Console.WriteLine($"{largeImage.GetType().Name} | {largeImage.Width:0}x{largeImage.Height:0} ({largeImage.PixelWidth}x{largeImage.PixelHeight}) {largeImage.DpiX:0}:{largeImage.DpiY:0}"); + } + if (buttonTiff.Image is System.Windows.Media.Imaging.BitmapSource smallImage) + { + System.Console.WriteLine($"{smallImage.GetType().Name} | {smallImage.Width:0}x{smallImage.Height:0} ({smallImage.PixelWidth}x{smallImage.PixelHeight}) {smallImage.DpiX:0}:{smallImage.DpiY:0}"); + } } ribbonPanel.RowStackedItems( diff --git a/ricaun.Revit.UI/BitmapExtension.cs b/ricaun.Revit.UI/BitmapExtension.cs index 55f5656..0cf2976 100644 --- a/ricaun.Revit.UI/BitmapExtension.cs +++ b/ricaun.Revit.UI/BitmapExtension.cs @@ -88,12 +88,22 @@ public static ImageSource Scale(this ImageSource imageSource, double scale) internal static double GetSystemDpi() { double systemDpi = 96; + try + { #if NET47_OR_GREATER || NET - var imageScaleInfo = VisualTreeHelper.GetDpi(new System.Windows.Controls.Image()); - systemDpi = imageScaleInfo.PixelsPerInchX; + var imageScaleInfo = VisualTreeHelper.GetDpi(new System.Windows.Controls.Image()); + systemDpi = imageScaleInfo.PixelsPerInchX; +#else + using (var g = System.Drawing.Graphics.FromHwnd(IntPtr.Zero)) + { + systemDpi = g.DpiX; + } #endif + } + catch { } return systemDpi; } + internal readonly static double SystemDpi = GetSystemDpi(); /// /// Get the bitmap frame from the based on the DPI and width. @@ -104,11 +114,17 @@ internal static double GetSystemDpi() /// The bitmap frame with the specified width or the smallest width frame. internal static BitmapFrame GetBitmapFrameByWidthAndDpi(this BitmapDecoder bitmapDecoder, int width = 0, int dpi = 0) { - double systemDpi = dpi > 0 ? dpi : GetSystemDpi(); + double systemDpi = dpi > 0 ? dpi : SystemDpi; + + double OrderDpiX(BitmapFrame frame) + { + var dpiX = Math.Round(frame.DpiX); + return dpiX >= systemDpi ? -systemDpi / dpiX : systemDpi / dpiX; + } var frames = bitmapDecoder.Frames; var frame = frames - .OrderBy(e => e.DpiX >= systemDpi ? -systemDpi / e.DpiX : systemDpi / e.DpiX) + .OrderBy(OrderDpiX) .ThenBy(e => e.Width) .FirstOrDefault(e => Math.Round(e.Width) >= width);