WPF 图片灰度处理

简介: 原文:WPF 图片灰度处理文章的内容是来自微软中文技术论坛的一个帖子,当时是想将一段将图片灰度处理的代码转换为XAML的一个样式,在这里要谢谢 Xiao Yan Qiang、Sheldon _Xiao、shixin的热情回答,现在将他们的回答贴出来供大家学习参考.
原文: WPF 图片灰度处理

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


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

 

提问: 这个功能如何写成一个样式,将一个窗体内所有的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" 
 
        <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: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 Function
End Class

 

 

再次的谢谢他们…

目录
相关文章
|
6月前
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
选中项目,点击右上角的显示全部文件按钮,会将默认隐藏的文件显示出来,选中所需图片,右键,添加到项目,然后选择图片查看属性,生成操作选择resource。完毕。本人目前的解决方案。
260 41
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
|
C#
WPF Image Source 设置相对路径图片
原文:WPF Image Source 设置相对路径图片   BitmapImage bt = new BitmapImage(new Uri("Images\\3_u10484.png", UriKind.Relative));this.Img1.Source = bt;
3815 0
|
C#
WPF 将图片进行灰度处理
原文:WPF 将图片进行灰度处理 处理前:      处理后:   这个功能使用使用了 FormatConvertedBitmap(为BitmapSource提供像素格式转换功能)   代码如下:   public partial class MainWindow : Window ...
972 0
|
算法 容器 数据可视化
WPF_界面_图片/界面/文字模糊解决之道整理
原文:WPF_界面_图片/界面/文字模糊解决之道整理 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010265681/article/details/76651792 图片模糊: 图片尺寸:  检查图片,png, DPI=72,Stretch="None",原图尺寸和xaml里面写的尺寸一致。
1202 0
|
C#
WPF 调用资源图片
原文:WPF 调用资源图片      最近做的wpf项目中,在开发的时候,把图片放到了bin下面,采用了imagePath =System.IO.Directory.GetCurrentDirectory()+"/images/starShow.
1261 0
|
C#
【C#/WPF】Image图片的Transform变换:平移、缩放、旋转
原文:【C#/WPF】Image图片的Transform变换:平移、缩放、旋转 WPF中图像控件Image的变换属性Transform: 平移 缩放 旋转 即要想实现图片的平移、缩放、旋转,是修改它所在的Image控件的Transform变换属性。
4737 0
|
C#
【C#/WPF】图片的切割/切图/裁剪图片
原文:【C#/WPF】图片的切割/切图/裁剪图片 前台准备两个Image控件。上面是显示原图,下面显示切割后的效果。 对应的后台代码: public par...
2120 0
|
C# 图形学
在WPF里面实现以鼠标位置为中心缩放移动图片
原文:在WPF里面实现以鼠标位置为中心缩放移动图片 在以前的文章使用WPF Resource以及Transform等技术实现鼠标控制图片缩放和移动的效果里面,介绍了如何在WPF里面移动和放大缩小图片,程序也支持使用滚轮的方式缩放图片。
1597 0
|
C# UED 自然语言处理
在WPF中实现图片一边下载一边显示
原文 在WPF中实现图片一边下载一边显示 当我们上网查看一个较大的图片时,浏览器能一边下载一边显示,这样用户体验是比较好的,但在WPF程序中,当我们通过如下方式显示一幅图片时:     img.Source = new BitmapImage(new Uri("http://localhost:8000/www/test.jpg")); 只能等到图片下载完成时才能显示出来,当图片较大时需要等待很久,即使在旁边放个进度条给人的感觉仍然不好。
1027 0
|
18天前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库