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)
상속: Image
예제 #1
0
        protected static void ApplyGreyScaleImage(GreyableImage greyScaleImg, Boolean isEnabled)
        {
            try
            {
                if (!isEnabled)
                {
                    if (greyScaleImg.Source is FormatConvertedBitmap)
                    {
                        // Already grey !
                        return;
                    }
                    var image       = greyScaleImg.Source as BitmapSource;
                    var bitmapImage = image ?? new BitmapImage(new Uri(greyScaleImg.Source.ToString()));
                    var conv        = new FormatConvertedBitmap(bitmapImage, PixelFormats.Gray32Float, null, 0);
                    greyScaleImg.Source = conv;

                    // Create Opacity Mask for greyscale image as FormatConvertedBitmap does not keep transparency info
                    greyScaleImg.OpacityMask = new ImageBrush(((FormatConvertedBitmap)greyScaleImg.Source).Source); //equivalent to new ImageBrush(bitmapImage)
                }
                else
                {
                    if (greyScaleImg.Source is FormatConvertedBitmap)
                    {
                        greyScaleImg.Source = ((FormatConvertedBitmap)greyScaleImg.Source).Source;
                    }
                    else if (greyScaleImg.Source is BitmapSource)
                    {
                        // Should be full color already.
                        return;
                    }

                    // Reset the Opcity Mask
                    greyScaleImg.OpacityMask = null;
                }
            }
            catch (Exception)
            {
                // nothin'
            }
        }
예제 #2
0
        protected static void ApplyGreyScaleImage(GreyableImage greyScaleImg, Boolean isEnabled)
        {
            try
            {
                if (!isEnabled)
                {
                    if (greyScaleImg.Source is FormatConvertedBitmap)
                    {
                        // Already grey !
                        return;
                    }
                    var image = greyScaleImg.Source as BitmapSource;
                    var bitmapImage = image ?? new BitmapImage(new Uri(greyScaleImg.Source.ToString()));
                    var conv = new FormatConvertedBitmap(bitmapImage, PixelFormats.Gray32Float, null, 0);
                    greyScaleImg.Source = conv;

                    // Create Opacity Mask for greyscale image as FormatConvertedBitmap does not keep transparency info
                    greyScaleImg.OpacityMask = new ImageBrush(((FormatConvertedBitmap)greyScaleImg.Source).Source); //equivalent to new ImageBrush(bitmapImage)
                }
                else
                {
                    if (greyScaleImg.Source is FormatConvertedBitmap)
                    {
                        greyScaleImg.Source = ((FormatConvertedBitmap)greyScaleImg.Source).Source;
                    }
                    else if (greyScaleImg.Source is BitmapSource)
                    {
                        // Should be full color already.
                        return;
                    }

                    // Reset the Opcity Mask
                    greyScaleImg.OpacityMask = null;
                }
            }
            catch (Exception)
            {
                // nothin'
            }
        }