Skip to content

Commit

Permalink
Add GetSystemDpi
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Nov 11, 2024
1 parent c665598 commit 45a6d35
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Support `tiff` files for icon.
### Updated
- Update `GetBitmapFrame` to round `width` that is changed by `dpi`.
- Add `GetBitmapFrameByDpiAndWidth` to get optimal frame by `dpi` and `width`.
- Add `GetBitmapFrameByWidthAndDpi` to get optimal frame by `dpi` and `width`.
- Add `GetSystemDpi` to get the system `dpi` on the fly.
### Example
- Add `Cube-Grey-Light.tiff` and `Cube-Grey-Dark.tiff` in `AppTheme`.

Expand Down
25 changes: 17 additions & 8 deletions ricaun.Revit.UI/BitmapExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,28 @@ public static ImageSource Scale(this ImageSource imageSource, double scale)
}

/// <summary>
/// Get the bitmap frame from the <paramref name="bitmapDecoder"/> based on the DPI and width.
/// Get the system DPI based on the <see cref="System.Windows.Media.VisualTreeHelper.GetDpi"/> using a new <see cref="System.Windows.Controls.Image"/>.
/// </summary>
/// <param name="bitmapDecoder">The bitmap decoder.</param>
/// <param name="width">The desired width of the bitmap frame. When set to zero, the smallest width frame is returned.</param>
/// <returns>The bitmap frame with the specified width or the smallest width frame.</returns>
internal static BitmapFrame GetBitmapFrameByDpiAndWidth(this BitmapDecoder bitmapDecoder, int width = 0)
internal static double GetSystemDpi()
{
double systemDpi = 96;

#if NET47_OR_GREATER || NET
var imageScaleInfo = VisualTreeHelper.GetDpi(new System.Windows.Controls.Image());
systemDpi = imageScaleInfo.PixelsPerInchX;
#endif
return systemDpi;
}

/// <summary>
/// Get the bitmap frame from the <paramref name="bitmapDecoder"/> based on the DPI and width.
/// </summary>
/// <param name="bitmapDecoder">The bitmap decoder.</param>
/// <param name="width">The desired width of the bitmap frame. When set to zero, the smallest width frame is returned.</param>
/// <param name="dpi">The optimal dpi for the frame.</param>
/// <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();

var frames = bitmapDecoder.Frames;
var frame = frames
Expand Down Expand Up @@ -134,7 +143,7 @@ TImageSource ScaleDownIfWidthIsGreater(TImageSource imageSource, int width)
{
bitmapFrame.DownloadCompleted += (s, e) =>
{
if (bitmapFrame.Decoder.GetBitmapFrameByDpiAndWidth(width) is TImageSource frame)
if (bitmapFrame.Decoder.GetBitmapFrameByWidthAndDpi(width) is TImageSource frame)
imageSource = frame;

imageSource = ScaleDownIfWidthIsGreater(imageSource, width);
Expand All @@ -143,7 +152,7 @@ TImageSource ScaleDownIfWidthIsGreater(TImageSource imageSource, int width)
};
}

if (bitmapFrame.Decoder.GetBitmapFrameByDpiAndWidth(width) is TImageSource frame)
if (bitmapFrame.Decoder.GetBitmapFrameByWidthAndDpi(width) is TImageSource frame)
imageSource = frame;
}

Expand Down

0 comments on commit 45a6d35

Please sign in to comment.