2009-06-06 31 views
3

我需要禁用時顯示爲灰色的圖像(IsEnabled = False)。通過將BitmapImage讀取到顯示爲here的FormatConvertedBitmap,可以生成圖像的灰色版本。如何使WPF圖像無法使用?

我已經能夠得到這個UserControl的工作,但現在我想在一個專門的圖像類相同的行爲更靈活。我不關心這是在XAML,代碼隱藏還是兩者都實現的,但它需要是Image的子類。

的使用可能是:

<DisableableImage Source="Images/image1.png" /> 
<DisableableImage Source="Images/image1.png" IsEnabled="False" /> 

<!-- Since IsEnabled is inherited down the tree, 
    the image will be grayed out like the rest of the button --> 
<Button IsEnabled="False"> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock>OK</TextBlock> 
     <DisableableImage Source="Images/ok.png" /> 
    </StackPanel> 
</Button> 
+0

可能重複http://stackoverflow.com/questions/4304972/is-there-a-way-to-desaturate- an-image-on-a-button-thats-disabled) – Justin 2014-04-29 18:26:47

回答

9

看一看this link

編輯: 或者this one(所有你需要的是AutoGreyableImage類)

+0

謝謝!但是我沒有準備好爲這個班級購買整個Infragistics Win Client軟件包:) – Oskar 2009-06-06 14:11:42

+0

對不起,我沒有意識到這是一個Infragistics控件...我有另一個鏈接,我會更新我的答案 – 2009-06-06 14:49:27

0

創建DisableableImage類,它是一個典型的WPF控制。在裏面放置兩個元素:圖像和一個矩形,只有當控件被禁用時纔會出現。矩形應該與圖像的寬度和高度相同,並且應該覆蓋圖像。使用灰色和約40%的阿爾法顏色,您應該獲得類似於實際淡化圖像的效果 - 而無需實際修改圖像本身。

2

如果您使用此功能,請考慮創建.NET 3.5 SP1(而非bitmapeffect)引入的自定義效果,以在GPU上渲染此類操作。這個效果可以很容易地被觸發器控制。

5

我根據以下解決方案做了一些比較。通過托馬斯Levesque的

  • Greyscale Effect
  • 提供由OP提供

    • 中鏈接的方法
    • 的聯繫

      由於我已經爲Infragistics的網絡優勢授予許可的WPF很容易嘗試一下

      下面是結果

      enter image description here

      所以最好的方法取決於什麼結果你在之後。至於我,我認爲Infragistics的AutoDisabledImage產生的結果太亮,AutoGreyableImage做得不錯(與方法1(OP鏈接)相同的結果)GreyscaleEffect產生最好的結果。

    2

    AutoGreyableImage的更完整版本,作者Thomas Lebrun。對於任何感興趣的人,我開始使用Thomas Lebruns類,並遇到了幾個null引用異常,並發現如果isEnabled屬性設置爲第一個,而源設置爲之後則不會禁用圖像。

    所以這裏是最終爲我做的伎倆的類。關於這一點,你當然可以將不透明的問題添加進去,但我決定把它留給圖像周圍的xaml。

    using System; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Media.Imaging; 
    using System.Windows.Media; 
    
    namespace MyDisabledImages 
    { 
        /// <summary> 
        /// Class used to have an image that is able to be gray when the control is not enabled. 
        /// Based on the version by 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 and Source property. 
          IsEnabledProperty.OverrideMetadata(typeof(AutoGreyableImage), new FrameworkPropertyMetadata(true, new PropertyChangedCallback(OnAutoGreyScaleImageIsEnabledPropertyChanged))); 
          SourceProperty.OverrideMetadata(typeof(AutoGreyableImage), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnAutoGreyScaleImageSourcePropertyChanged))); 
         } 
    
         protected static AutoGreyableImage GetImageWithSource(DependencyObject source) 
         { 
          var image = source as AutoGreyableImage; 
          if (image == null) 
           return null; 
    
          if (image.Source == null) 
           return null; 
    
          return image; 
         } 
    
         /// <summary> 
         /// Called when [auto grey scale image source property changed]. 
         /// </summary> 
         /// <param name="source">The source.</param> 
         /// <param name="args">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> 
         protected static void OnAutoGreyScaleImageSourcePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs ars) 
         { 
          AutoGreyableImage image = GetImageWithSource(source); 
          if (image != null) 
           ApplyGreyScaleImage(image, image.IsEnabled); 
         } 
    
         /// <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> 
         protected static void OnAutoGreyScaleImageIsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args) 
         { 
          AutoGreyableImage image = GetImageWithSource(source); 
          if (image != null) 
          { 
           var isEnabled = Convert.ToBoolean(args.NewValue); 
           ApplyGreyScaleImage(image, isEnabled); 
          } 
         } 
    
         protected static void ApplyGreyScaleImage(AutoGreyableImage autoGreyScaleImg, Boolean isEnabled) 
         { 
          try 
          { 
           if (!isEnabled) 
           { 
            BitmapSource bitmapImage = null; 
    
            if (autoGreyScaleImg.Source is FormatConvertedBitmap) 
            { 
             // Already grey ! 
             return; 
            } 
            else if (autoGreyScaleImg.Source is BitmapSource) 
            { 
             bitmapImage = (BitmapSource)autoGreyScaleImg.Source; 
            } 
            else // trying string 
            { 
             bitmapImage = new BitmapImage(new Uri(autoGreyScaleImg.Source.ToString())); 
            } 
            FormatConvertedBitmap conv = new FormatConvertedBitmap(bitmapImage, PixelFormats.Gray32Float, null, 0); 
            autoGreyScaleImg.Source = conv; 
    
            // Create Opacity Mask for greyscale image as FormatConvertedBitmap does not keep transparency info 
            autoGreyScaleImg.OpacityMask = new ImageBrush(((FormatConvertedBitmap)autoGreyScaleImg.Source).Source); //equivalent to new ImageBrush(bitmapImage) 
           } 
           else 
           { 
            if (autoGreyScaleImg.Source is FormatConvertedBitmap) 
            { 
             autoGreyScaleImg.Source = ((FormatConvertedBitmap)autoGreyScaleImg.Source).Source; 
            } 
            else if (autoGreyScaleImg.Source is BitmapSource) 
            { 
             // Should be full color already. 
             return; 
            } 
    
            // Reset the Opcity Mask 
            autoGreyScaleImg.OpacityMask = null; 
           } 
          } 
          catch (Exception) 
          { 
           // nothin' 
          } 
    
         } 
    
        } 
    } 
    
    的[有沒有辦法去色上禁用的按鈕,多數民衆贊成的圖像?](