WPF DPI 缩放转换器
1、由于好多笔记本电脑都会缩放屏幕具体可查

wpf的尺寸就会放大1.25倍,好多固定长度宽度都会收到影响,目前我只能把放大的 给缩小回去
public class DpiConverter : IValueConverter { /// <summary> /// 将设计时尺寸转换为物理像素尺寸 /// </summary> public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { // 获取当前DPI缩放比例 Matrix matrix = PresentationSource.FromVisual(Application.Current.MainWindow) .CompositionTarget.TransformToDevice; double dpiScaleX = matrix.M11; double dpiScaleY = matrix.M22; // 处理输入值 if (value is double doubleValue) { // 使用参数控制方向(默认横向缩放) if (parameter is string dir && dir.ToLower() == "y") return doubleValue / dpiScaleY; return doubleValue / dpiScaleX; } if (value is int intValue) { return intValue / dpiScaleX; } return DependencyProperty.UnsetValue; } /// <summary> /// 将物理像素尺寸转换回设计时尺寸 /// </summary> public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { Matrix matrix = PresentationSource.FromVisual(Application.Current.MainWindow) .CompositionTarget.TransformToDevice; double dpiScaleX = matrix.M11; double dpiScaleY = matrix.M22; if (value is double doubleValue) { if (parameter is string dir && dir.ToLower() == "y") return doubleValue * dpiScaleY; return doubleValue * dpiScaleX; } if (value is int intValue) { return intValue * dpiScaleX; } return DependencyProperty.UnsetValue; } /// <summary> /// 获取当前DPI缩放比例(静态访问) /// </summary> public static (double scaleX, double scaleY) GetDpiScaling() { Matrix matrix = PresentationSource.FromVisual(Application.Current.MainWindow) .CompositionTarget.TransformToDevice; return (matrix.M11, matrix.M22); } }
亲测可用
不想平凡,奈何太懒 T_T

浙公网安备 33010602011771号