0

XBAP WPF アプリケーションで複数のユーザーの dpi 設定でベクターではなく、ビットマップ画像を表示する必要があるため、起動時に dpiFactor グローバル変数を設定したいと思います。これは、元の bitmSizeap のパーセンテージとして計算されます。

つまり、120 dpi の場合、画像の両方のサイズを次のようにしたいと考えています。

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 に答える