From 8f50d6e6169cf511e9370022aa7f7c3a14912b89 Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Tue, 19 Nov 2024 08:03:15 -0300 Subject: [PATCH] Update `UriToBitmapFrame` to use `GetBitmapFrameByWidthAndDpi` with `int.MaxValue` --- CHANGELOG.md | 2 ++ Directory.Build.props | 2 +- .../Resources/ResourceTiffTests.cs | 25 ++++++++++++++++--- ricaun.Revit.UI/BitmapExtension.cs | 12 ++++++--- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4128c16..2f498c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Update `GetBitmapFrameByWidthAndDpi` to round `dpi` frame value. - Update `Width` to `Math.Round` to improve order by `Width`. - Update `GetBitmapFrameByWidthAndDpi` to public. +- Update `GetBitmapFrameByWidthAndDpi` to return last `Width`. +- Update `UriToBitmapFrame` to use `GetBitmapFrameByWidthAndDpi` with `int.MaxValue`. ### Example - Add `Cube-Grey-Light.tiff` and `Cube-Grey-Dark.tiff` in `AppTheme`. ### Tests diff --git a/Directory.Build.props b/Directory.Build.props index 776e1de..72ef80b 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 0.7.1-alpha.4 + 0.7.1-alpha.5 \ No newline at end of file diff --git a/ricaun.Revit.UI.Tests/Resources/ResourceTiffTests.cs b/ricaun.Revit.UI.Tests/Resources/ResourceTiffTests.cs index b5dcc29..8198cfe 100644 --- a/ricaun.Revit.UI.Tests/Resources/ResourceTiffTests.cs +++ b/ricaun.Revit.UI.Tests/Resources/ResourceTiffTests.cs @@ -28,12 +28,14 @@ public void GetBitmapSource_NotNull() Assert.IsNotNull(("/" + ResourceNameTiff).GetBitmapSource()); } - [TestCase(32, 384)] - public void GetBitmapSource_Default_WidthAndDpi(int width, int dpi) + [TestCase(32)] + public void GetBitmapSource_Default_Width(int width) { var source = ResourceNameTiff.GetBitmapSource(); Assert.AreEqual(width, Math.Round(source.Width)); - Assert.AreEqual(dpi, Math.Round(source.DpiX)); + var systemDpi = BitmapExtension.SystemDpi; + if (systemDpi != Math.Round(source.DpiX)) + Assert.Ignore($"SystemDpi:{systemDpi} != {Math.Round(source.DpiX)}"); } [TestCase(10)] @@ -72,7 +74,7 @@ public void BitmapFrame_ByWidthAndDpi(int width, int dpi) [TestCase(32, 336, 384)] // 3.5 [TestCase(16, 480, 384)] // 5.0 [TestCase(32, 480, 384)] // 5.0 - public void BitmapFrame_ByWidthAndDpi_Expected(int width, int dpi, int dpiExpected) + public void BitmapFrame_ByWidthAndDpi_DpiExpected(int width, int dpi, int dpiExpected) { Assert.IsNotNull(BitmapFrame); var decoder = BitmapFrame.Decoder; @@ -81,5 +83,20 @@ public void BitmapFrame_ByWidthAndDpi_Expected(int width, int dpi, int dpiExpect Assert.AreEqual(width, Math.Round(frame.Width)); Assert.AreEqual(dpiExpected, Math.Round(frame.DpiX)); } + + [TestCase(64, 96, 32)] // 1.0 + [TestCase(64, 144, 32)] // 1.5 + [TestCase(64, 192, 32)] // 2.0 + [TestCase(64, 288, 32)] // 3.0 + [TestCase(64, 384, 32)] // 4.0 + public void BitmapFrame_ByWidthAndDpi_WidthExpected(int width, int dpi, int widthExpected) + { + Assert.IsNotNull(BitmapFrame); + var decoder = BitmapFrame.Decoder; + var frame = decoder.GetBitmapFrameByWidthAndDpi(width, dpi) as System.Windows.Media.Imaging.BitmapFrame; + Assert.IsNotNull(frame); + Assert.AreEqual(widthExpected, Math.Round(frame.Width)); + Assert.AreEqual(dpi, Math.Round(frame.DpiX)); + } } } \ No newline at end of file diff --git a/ricaun.Revit.UI/BitmapExtension.cs b/ricaun.Revit.UI/BitmapExtension.cs index 757497c..e0d7818 100644 --- a/ricaun.Revit.UI/BitmapExtension.cs +++ b/ricaun.Revit.UI/BitmapExtension.cs @@ -17,7 +17,7 @@ internal static BitmapFrame UriToBitmapFrame(string uriString) { var uri = new Uri(uriString, UriKind.RelativeOrAbsolute); var decoder = BitmapDecoder.Create(uri, BitmapCreateOptions.None, BitmapCacheOption.Default); - return decoder.Frames.OrderBy(e => Math.Round(e.Width)).LastOrDefault(); + return decoder.GetBitmapFrameByWidthAndDpi(int.MaxValue); } /// @@ -133,11 +133,15 @@ double OrderDpiX(BitmapFrame frame) } var frames = bitmapDecoder.Frames; - var frame = frames + var framesOrder = frames .OrderBy(OrderDpiX) - .ThenBy(e => Math.Round(e.Width)) - .FirstOrDefault(e => Math.Round(e.Width) >= width); + .ThenBy(e => Math.Round(e.Width)); + var widthMax = (int)Math.Round(framesOrder.LastOrDefault().Width); + if (width > widthMax) + width = widthMax; + + var frame = framesOrder.FirstOrDefault(e => Math.Round(e.Width) >= width); return frame; }