Skip to content

Commit

Permalink
Update GetBitmapFrameByWidthAndDpi to round dpi frame value.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Nov 16, 2024
1 parent 03e5481 commit 585d09b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

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.1</Version>
<Version>0.7.1-alpha.2</Version>
</PropertyGroup>
</Project>
16 changes: 8 additions & 8 deletions ricaun.Revit.UI.Example/Revit/AppTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ public Result OnStartup(UIControlledApplication application)
{
var buttonTiff = ribbonPanel.CreatePushButton<CommandTheme>("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(
Expand Down
24 changes: 20 additions & 4 deletions ricaun.Revit.UI/BitmapExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/// <summary>
/// Get the bitmap frame from the <paramref name="bitmapDecoder"/> based on the DPI and width.
Expand All @@ -104,11 +114,17 @@ internal static double GetSystemDpi()
/// <returns>The bitmap frame with the specified width or the smallest width frame.</returns>
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);

Expand Down

0 comments on commit 585d09b

Please sign in to comment.