Article From:https://www.cnblogs.com/lonelyxmas/p/9689168.html
WPF image grayscale processing

The content of this article is a post from Microsoft Chinese Technology Forum, which was intended to convert a grayscale image processing code into a XAML style. Thank you here.

Xiao Yan Qiang、Sheldon _Xiao、shixinFor their enthusiastic responses, they will now be posted for reference.

 

Question: How does this function write as a style that converts the image format of all Image controls in a form to Gray8

 

BitmapImage bitmapImage = new BitmapImage(new Uri(“D:\\Face.jpg”));

FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
newFormatedBitmapSource.BeginInit();
newFormatedBitmapSource.Source = bitmapImage;
newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray8;
newFormatedBitmapSource.EndInit();

img.Source = newFormatedBitmapSource;
this.Content = img;

 

 

Xiao Yan QiangThe answer:

 

public class ImageAttached
{
    // Gray8Additional attributes, Gary8 image style "switch"
public static readonly DependencyProperty Gray8Property =
        DependencyProperty.RegisterAttached("Gray8", typeof(bool), typeof(ImageAttached),
            new FrameworkPropertyMetadata((bool)false,
                new PropertyChangedCallback(OnGray8Changed)));

    public static bool GetGray8(DependencyObject d)
    {
        return (bool)d.GetValue(Gray8Property);
    }

    public static void SetGray8(DependencyObject d, bool value)
    {
        d.SetValue(Gray8Property, value);
    }

    private static void OnGray8Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Image currentImage = d as Image;
        if (currentImage == null)
        {
            return;
        }

        bool isGray8 = (bool)d.GetValue(Gray8Property);

        if (isGray8)
        {
            // Add the BitmapSourceBackup attribute to backup the current BitmapSource for recovery purposes.
BitmapSource backupBitmapSource = (currentImage.Source as BitmapSource).CloneCurrentValue();
            d.SetValue(BitmapSourceBackupProperty, backupBitmapSource);

            // Establishing BitmapSource of Gray8
FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
            newFormatedBitmapSource.BeginInit();
            newFormatedBitmapSource.Source = currentImage.Source as BitmapSource;
            newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray8;
            newFormatedBitmapSource.EndInit();

            // Replace ImageSource
currentImage.Source = newFormatedBitmapSource;
        }
        else
        {
            // Image recovery operation
object obj = currentImage.GetValue(BitmapSourceBackupProperty);
            if (obj == null)
            {
                return;
            }

            BitmapSource bs = obj as BitmapSource;
            if (bs == null)
            {
                return;
            }

            currentImage.Source = bs;
        }
    }

    // Backup the additional attributes of the source image, automatically add when Gray8 changes.
public static readonly DependencyProperty BitmapSourceBackupProperty =
        DependencyProperty.RegisterAttached("BitmapSourceBackup", typeof(BitmapSource), typeof(ImageAttached),
            new FrameworkPropertyMetadata(null));

    public static BitmapSource GetBitmapSourceBackup(DependencyObject d)
    {
        return (BitmapSource)d.GetValue(BitmapSourceBackupProperty);
    }

    public static void SetBitmapSourceBackup(DependencyObject d, BitmapSource value)
    {
        d.SetValue(BitmapSourceBackupProperty, value);
    }
}
Then add local:ImageAttached.Gray8= "True" in XAML.
 
        <Image xmlns:local="clr-namespace:WpfImageGray8Sample" Source="/WpfImageGray8Sample;component/Images/44537119.jpg" local:ImageAttached.Gray8="True" />
 
This way we can control Gray8 conveniently.
 

 
Sheldon _Xiao

The answer:

 
A link is recommended: http://www.rikware.com/post/Setting-Format Converted Bitmap-Source-via-Binding.aspx
 
It is also implemented with a converter.
 
 

shixinThe answer:

 

<Window x:Class=”MainWindow” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:y=”clr-namespace:WpfApplication1″  Title=”MainWindow” Height=”350″ Width=”525″>

            <
Window.Resources>

                <
y:GrayImage x:Key=”GrayImage” />

            </
Window.Resources>

            <
Grid>

                <
Image Source=”{Binding RelativeSource={RelativeSource Self}, Path=Tag, Converter={StaticResource GrayImage}}” Tag=”C:\Test.jpg” />

            </
Grid>

        </
Window>

 

xamlIt can also be written in this way.

<Window.Resources>

            <
y:GrayImage x:Key=”GrayImage” />

            <
Style TargetType=”{x:Type Image}”>

                <
Setter Property=”Source” Value=”{Binding RelativeSource={RelativeSource Self}, Path=Tag, Converter={StaticResource GrayImage}}” />

            </
Style>

        </
Window.Resources>

        <
Grid>

            <
Image Tag=”C:\Test.jpg” />

        </
Grid>

 

Public Class GrayImage
    Implements IValueConverter
    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
        Try
            Dim newFormatedBitmapSource As New FormatConvertedBitmap
            newFormatedBitmapSource.BeginInit()
            newFormatedBitmapSource.Source = New BitmapImage(New Uri(value.ToString))
            newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray8
            newFormatedBitmapSource.EndInit()
            Convert = newFormatedBitmapSource
        Catch
            Convert = Nothing
        End Try
    End Function
    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        ConvertBack = value
    End Function
End Class

 

 

Thank them again.

Link of this Article: WPF image grayscale processing

Leave a Reply

Your email address will not be published. Required fields are marked *