0

必须显示位图图像 - 在 XBAP WPF 应用程序中的多个用户的 dpi 设置中不是矢量,我想在启动时设置一个 dpiFactor 全局变量,它将计算为原始 bitmSizeap 的百分比:

即对于 120 dpi,我希望图像的两个大小都是:newSize = originalSize * (100 - (120 - 96)) / 100,这意味着如果 dpi 是原始的 125%,则乘以 75%。

dpiFactor 必须在启动时定义,然后在页面启动时按比例缩小(或放大)所有测量值。我如何在 XAML 中使用绑定属性来表达它?

4

1 回答 1

-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,我认为您可以使用代码。

于 2011-05-08T12:43:05.203 回答