WPF 图片灰度处理

本文介绍了如何在WPF应用中通过XAML样式将所有Image控件的图片转换为灰度效果。引用了微软中文技术论坛上的讨论,提供了两种不同的XAML实现方式,一种是直接在Image元素中设置,另一种是定义Style应用于所有Image控件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章的内容是来自微软中文技术论坛的一个帖子,当时是想将一段将图片灰度处理的代码转换为XAML的一个样式,在这里要谢谢


Xiao Yan Qiang
Sheldon _Xiao、shixin 的热情回答,现在将他们的回答贴出来供大家学习参考.内容如下:

 

提问: 这个功能如何写成一个样式,将一个窗体内所有的Image控件的图片格式都转换为Gray8


BitmapImage bitmapImage = new BitmapImage(newUri("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 Qiang的回答:

 


public class ImageAttached
{
    // Gray8附加属性,Gary8图片样式的"开关"    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)
        {
            // 附加BitmapSourceBackup属性,备份当前BitmapSource,以备恢复用          
            BitmapSource backupBitmapSource = (currentImage.Source as BitmapSource).CloneCurrentValue();
            d.SetValue(BitmapSourceBackupProperty, backupBitmapSource);

            // 建立Gray8的BitmapSource            FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
            newFormatedBitmapSource.BeginInit();
            newFormatedBitmapSource.Source = currentImage.Source as BitmapSource;
            newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray8;
            newFormatedBitmapSource.EndInit();

            // 替换ImageSource            
            currentImage.Source = newFormatedBitmapSource;
        }
        else
        {
            // 图像恢复操作            
            object obj = currentImage.GetValue(BitmapSourceBackupProperty);
            if (obj == null)
            {
                return;
            }

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

            currentImage.Source = bs;
        }
    }

    // 备份用源图像的附加属性,当Gray8变更时,自动附加    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);
    }
}
 

 

然后XAML里添加 local:ImageAttached.Gray8="True" 
 
        <Image xmlns:local="clr-namespace:WpfImageGray8Sample" Source="/WpfImageGray8Sample;component/Images/44537119.jpg" local:ImageAttached.Gray8="True" />
 
这样就可以方便控制Gray8了。
 
 
 
Sheldon _Xiao的回答:
 
推荐了一个链接: http://www.rikware.com/post/Setting-FormatConvertedBitmap-Source-via-Binding.aspx
 
里面也是用转换器实现
 
 
 
 
 
 

shixin的回答:

<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:GrayImagex:Key="GrayImage" />    
            </
Window.Resources>    
            <
Grid>    
                <
Image Source="{BindingRelativeSource={RelativeSourceSelf}, Path=Tag,Converter={StaticResourceGrayImage}}" Tag="C:\Test.jpg" />    
            </
Grid>    
        </
Window>    

 

xaml部分还可以写成这样

<Window.Resources>    
            <
y:GrayImagex:Key="GrayImage" />    
            <
Style TargetType="{x:TypeImage}">    
                <
Setter Property="Source"Value="{BindingRelativeSource={RelativeSourceSelf}, Path=Tag,Converter={StaticResourceGrayImage}}" />    
            </
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


 

 
 

再次的谢谢他们…

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值