博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF 图片灰度处理
阅读量:5943 次
发布时间:2019-06-19

本文共 4816 字,大约阅读时间需要 16 分钟。

原文:

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

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

 

提问: 这个功能如何写成一个样式,将一个窗体内所有的Image控件的图片格式都转换为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 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"
 
 
这样就可以方便控制Gray8了。
 

 
 

的回答:

 
推荐了一个链接:
 
里面也是用转换器实现
 
 

的回答:

 

<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>

 

xaml部分还可以写成这样

<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 FunctionEnd Class

 

 

再次的谢谢他们…

转载地址:http://cczxx.baihongyu.com/

你可能感兴趣的文章
go语言基础
查看>>
【Windows】字符串处理
查看>>
Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)
查看>>
CentOS使用chkconfig增加开机服务提示service xxx does not support chkconfig的问题解决
查看>>
微服务+:服务契约治理
查看>>
save
查看>>
Android DrawLayout + ListView 的使用(一)
查看>>
clear session on close of browser jsp
查看>>
asp.net mvc Post上传文件大小限制 (转载)
查看>>
关于吃掉物理的二次聚合无法实现的需要之旁门左道实现法
查看>>
mysql出现unblock with 'mysqladmin flush-hosts'
查看>>
oracle exp/imp命令详解
查看>>
开发安全的 API 所需要核对的清单
查看>>
Mycat源码中的单例模式
查看>>
WPF Dispatcher介绍
查看>>
fiddler展示serverIP方法
查看>>
C语言中的随意跳转
查看>>
WPF中如何将ListViewItem双击事件绑定到Command
查看>>
《聚散两依依》
查看>>
小tips:你不知道的 npm init
查看>>