必须显示位图图像 - 不是XBAP WPF应用程序中多个用户dpi设置的矢量,我想在启动时设置一个dpiFactor全局变量,它将按原始bitmSizeap的百分比计算:
即。为120 dpi我希望图像的大小都是:newSize = originalSize *(100 - (120 - 96))/ 100 这意味着如果dpi是原始值的125%,则乘以75%。
必须在启动时定义dpiFactor,然后在启动页面时缩小(或向上)所有测量。 我怎样才能在XAML中用绑定属性表达它?
答案 0 :(得分:-1)
也许您可以使用看起来像这样的转换器:
[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();
}
}
在xaml中,这将是这样的:
<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,我认为您可以使用this代码。