wpf绑定属性以在高dpi下调整bmp的大小

发布于 2024-11-05 21:54:53 字数 288 浏览 0 评论 0原文

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

即我想要的 120 dpi图像的大小均为: newSize = originalSize * (100 - (120 - 96)) / 100 这意味着如果 dpi 是原始值的 125%,则乘以 75%。

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

Having to display bitmap images - not vector at several user's dpi settings in a XBAP WPF application, I'd like to setup a dpiFactor global variable at startup, that will be calculated as a percentage of the original bitmSizeap:

i.e. for 120 dpi I want both size of the image to be: newSize = originalSize * (100 - (120 - 96)) / 100
which means multiply by 75% if the dpi is 125% of original.

The dpiFactor have to be defined at startup, and then all measurement to be scaled down (or up) when page is launched.
How can I express that in XAML perhaps with a bound property?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼睛会笑 2024-11-12 21:54:53

也许您可以使用如下所示的转换器:

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

Maybe you can use a converter that looks like this:

  [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();
    }
  }

And in xaml this will looks like this:

<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>

To get the system's dpi i think you can use this code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文