Skip to content

Commit

Permalink
Update UriToBitmapFrame to use GetBitmapFrameByWidthAndDpi with `…
Browse files Browse the repository at this point in the history
…int.MaxValue`
  • Loading branch information
ricaun committed Nov 19, 2024
1 parent c41eb02 commit 8f50d6e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>0.7.1-alpha.4</Version>
<Version>0.7.1-alpha.5</Version>
</PropertyGroup>
</Project>
25 changes: 21 additions & 4 deletions ricaun.Revit.UI.Tests/Resources/ResourceTiffTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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;
Expand All @@ -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));
}
}
}
12 changes: 8 additions & 4 deletions ricaun.Revit.UI/BitmapExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 8f50d6e

Please sign in to comment.