namespace AntDesignWPF.Utils
{
using System.Windows;
using System.Windows.Media;
public static class DpiUtil
{
public static DpiScale GetDpi(Visual visual)
{
#if NET40 || NET45
var source = PresentationSource.FromVisual(visual);
if (source?.CompositionTarget == null)
{
return new DpiScale(1.0, 1.0);
}
var device = source.CompositionTarget.TransformToDevice;
return new DpiScale(device.M11, device.M22);
#else
return VisualTreeHelper.GetDpi(visual);
#endif
}
}
#if NET40 || NET45
/// Stores DPI information from which a or is rendered.
public struct DpiScale
{
/// Gets the DPI scale on the X axis.
/// The DPI scale for the X axis.
public double DpiScaleX { get; private set; }
/// Gets the DPI scale on the Yaxis.
/// The DPI scale for the Y axis.
public double DpiScaleY { get; private set; }
/// Get or sets the PixelsPerDip at which the text should be rendered.
/// The current value.
public double PixelsPerDip
{
get
{
return DpiScaleY;
}
}
/// Gets the DPI along X axis.
/// The DPI along the X axis.
public double PixelsPerInchX
{
get
{
return 96.0 * DpiScaleX;
}
}
/// Gets the DPI along Y axis.
/// The DPI along the Y axis.
public double PixelsPerInchY
{
get
{
return 96.0 * DpiScaleY;
}
}
/// Initializes a new instance of the structure.
/// The DPI scale on the X axis.
/// The DPI scale on the Y axis.
public DpiScale(double dpiScaleX, double dpiScaleY)
{
DpiScaleX = dpiScaleX;
DpiScaleY = dpiScaleY;
}
}
#endif
}