wpf bind属性在高dpi中调整bmp的大小
在XBAP WPF应用程序中,必须在多个用户的dpi设置下显示位图图像-而不是矢量,我想在启动时设置一个dpiFactor全局变量,该变量将以原始bitmSizeap的百分比计算: i、 对于120 dpi,我希望图像的两个大小都是:newSize=originalSize*(100-(120-96))/100 也就是说,如果dpi为原始值的125%,则乘以75% 必须在启动时定义dpiFactor,然后在启动页面时缩小(或增大)所有测量。wpf bind属性在高dpi中调整bmp的大小,wpf,bitmap,resize,highdpi,Wpf,Bitmap,Resize,Highdpi,在XBAP WPF应用程序中,必须在多个用户的dpi设置下显示位图图像-而不是矢量,我想在启动时设置一个dpiFactor全局变量,该变量将以原始bitmSizeap的百分比计算: i、 对于120 dpi,我希望图像的两个大小都是:newSize=originalSize*(100-(120-96))/100 也就是说,如果dpi为原始值的125%,则乘以75% 必须在启动时定义dpiFactor,然后在启动页面时缩小(或增大)所有测量。 我如何在XAML中用绑定属性表示呢 也许您可以使用如下
我如何在XAML中用绑定属性表示呢 也许您可以使用如下所示的转换器:
[ValueConversion(typeof(string), typeof(BitmapImage))]
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string imageSource = value as string;
if (imageSource == null)
return DependencyProperty.UnsetValue;
try
{
BitmapImage originalImage = new BitmapImage(new Uri(imageSource));
int originalWidth = originalImage.PixelWidth;
int originalHeight = originalImage.PixelHeight;
double originalDpiX = originalImage.DpiX;
double originalDpiY = originalImage.DpiY;
BitmapImage scaledImage = new BitmapImage();
scaledImage.BeginInit();
scaledImage.DecodePixelWidth = originalWidth; // Place your calculation here,
scaledImage.DecodePixelHeight = originalHeight; // and here.
scaledImage.UriSource = new Uri(imageSource);
scaledImage.EndInit();
scaledImage.Freeze();
return scaledImage;
}
catch
{
}
return new BitmapImage();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test="clr-namespace:Test">
<Window.Resources>
<test:ImageConverter x:Key="imageConverter" />
</Window.Resources>
<Image Source="{Binding SomePath, Converter={StaticResource imageConverter}}" />
</Window>
在xaml中,这将如下所示:
[ValueConversion(typeof(string), typeof(BitmapImage))]
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string imageSource = value as string;
if (imageSource == null)
return DependencyProperty.UnsetValue;
try
{
BitmapImage originalImage = new BitmapImage(new Uri(imageSource));
int originalWidth = originalImage.PixelWidth;
int originalHeight = originalImage.PixelHeight;
double originalDpiX = originalImage.DpiX;
double originalDpiY = originalImage.DpiY;
BitmapImage scaledImage = new BitmapImage();
scaledImage.BeginInit();
scaledImage.DecodePixelWidth = originalWidth; // Place your calculation here,
scaledImage.DecodePixelHeight = originalHeight; // and here.
scaledImage.UriSource = new Uri(imageSource);
scaledImage.EndInit();
scaledImage.Freeze();
return scaledImage;
}
catch
{
}
return new BitmapImage();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test="clr-namespace:Test">
<Window.Resources>
<test:ImageConverter x:Key="imageConverter" />
</Window.Resources>
<Image Source="{Binding SomePath, Converter={StaticResource imageConverter}}" />
</Window>
要获得系统的dpi,我认为可以使用代码