【问题标题】:Microsoft WPF Ribbon October 2010 - Bad Image QualityMicrosoft WPF 功能区 2010 年 10 月 - 图像质量差
【发布时间】:2012-01-12 12:50:54
【问题描述】:

WPF 功能区的图像质量很差。我加了

<Window.Resources>
    <Style TargetType="{x:Type Image}">
        <Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality" />
    </Style>
</Window.Resources>

在我的功能区窗口中 - 但它没有帮助。也尝试过

<ribbon:RibbonWindow.Resources>

图像质量仍然很差:-(

有人知道这个问题的解决方法吗?我的图像是 48x48 png,我将它们用于大图像图标

谢谢 迈克尔

【问题讨论】:

    标签: wpf ribboncontrolslibrary


    【解决方案1】:

    您是否查看过来自 Microsoft 的以下页面,其中包含有关图像大小/DPI 的具体建议?

    http://msdn.microsoft.com/en-us/library/windows/desktop/dd316921(v=vs.85).aspx

    【讨论】:

    • 48x48 像素也推荐在这个页面上(我显示大图标)。
    【解决方案2】:

    质量差的原因是图像尺寸不正确。小图标必须为 16 x 16。大图标必须为 32 x 32。如果图像尺寸为其他尺寸,则会被拉伸。并且拉伸会导致图像质量下降。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,我创建了自己的用户控件来解决这个问题。

      我就是这样做的:

      <ribbon:Ribbon>
              <ribbon:RibbonTab Header="File">
                  <ribbon:RibbonGroup Header="File">
                      <views:ImageButton Command="{Binding LoadCommand}" Caption="Open" SourceImage="/Images/save.png"/>
                  </ribbon:RibbonGroup>
              </ribbon:RibbonTab>
          </ribbon:Ribbon>
      

      图像按钮用户控件ImageButton.xaml

      <UserControl Name="control"
      
       <Button Command="{Binding Command, ElementName=control}" Style="{x:Null}">
          <StackPanel>
              <infrastructure:AutoGreyableImage Width="32" Source="{Binding SourceImage, ElementName=control}"/>
              <TextBlock Text="{Binding Caption, ElementName=control}"/>
          </StackPanel>
      </Button>
      

      图像按钮用户控件ImageButton.xaml.cs

      public partial class ImageButton : UserControl
      {
          public static DependencyProperty CommandProperty = DependencyProperty.Register(
            "Command", typeof(ICommand), typeof(ImageButton));
          public static DependencyProperty SourceProperty = DependencyProperty.Register(
            "SourceImage", typeof(string), typeof(ImageButton));
          public static DependencyProperty CaptionProperty = DependencyProperty.Register(
            "Caption", typeof(string), typeof(ImageButton));
      
          public ICommand Command
          {
              get
              {
                  return (ICommand)GetValue(CommandProperty);
              }
              set
              {
                  SetValue(CommandProperty, value);
              }
      
          }
          public string SourceImage
          {
              get
              {
                  return (string)GetValue(SourceProperty);
              }
              set
              {
                  SetValue(SourceProperty, value);
              }
      
          }
          public string Caption
          {
              get
              {
                  return (string)GetValue(CaptionProperty);
              }
              set
              {
                  SetValue(CaptionProperty, value);
              }
      
          }
          public ImageButton()
          {
              InitializeComponent();
          }
      }
      

      对于 AutogreyableImage,我使用了this post

      这是类的复制粘贴

          using System.Windows.Controls;
          using System.Windows.Media;
          using System.Windows.Media.Imaging;  
      
      
      /// <summary>
          /// Class used to have an image that is able to be gray when the control is not enabled.
          /// Author: Thomas LEBRUN (http://blogs.developpeur.org/tom)
          /// </summary>
          public class AutoGreyableImage : Image
          {
      
              /// <summary>
              /// Initializes a new instance of the <see cref="AutoGreyableImage"/> class.
              /// </summary>
              static AutoGreyableImage()
              {
                  // Override the metadata of the IsEnabled property.
                  IsEnabledProperty.OverrideMetadata(typeof(AutoGreyableImage), new FrameworkPropertyMetadata(true, new PropertyChangedCallback(OnAutoGreyScaleImageIsEnabledPropertyChanged)));
              }
      
              /// <summary>
              /// Called when [auto grey scale image is enabled property changed].
              /// </summary>
              /// <param name="source">The source.</param>
              /// <param name="args">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
              private static void OnAutoGreyScaleImageIsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
              {
                  var autoGreyScaleImg = source as AutoGreyableImage;
                  var isEnable = Convert.ToBoolean(args.NewValue);
                  if (autoGreyScaleImg != null)
                  {
                      if (!isEnable)
                      {
                          // Get the source bitmap
                          var bitmapImage = new BitmapImage(new Uri(autoGreyScaleImg.Source.ToString()));
                          // Convert it to Gray
                          autoGreyScaleImg.Source = new FormatConvertedBitmap(bitmapImage, PixelFormats.Gray32Float, null, 0);
                          // Create Opacity Mask for greyscale image as FormatConvertedBitmap does not keep transparency info
                          autoGreyScaleImg.OpacityMask = new ImageBrush(bitmapImage);
                      }
                      else
                      {
                          // Set the Source property to the original value.
                          autoGreyScaleImg.Source = ((FormatConvertedBitmap)autoGreyScaleImg.Source).Source;
                          // Reset the Opcity Mask
                          autoGreyScaleImg.OpacityMask = null;
                      }
                  }
              }
          }
      

      我希望这会帮助你和其他人来

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-27
        • 2020-10-08
        • 1970-01-01
        • 1970-01-01
        • 2011-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多