原文:WPF 图片灰度处理

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

Xiao Yan QiangSheldon _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的回答:

  1. public class ImageAttached
  2. {
  3. // Gray8附加属性,Gary8图片样式的"开关"
  1. public static readonly DependencyProperty Gray8Property =
  2. DependencyProperty.RegisterAttached("Gray8", typeof(bool), typeof(ImageAttached),
  3. new FrameworkPropertyMetadata((bool)false,
  4. new PropertyChangedCallback(OnGray8Changed)));
  5.  
  6. public static bool GetGray8(DependencyObject d)
  7. {
  8. return (bool)d.GetValue(Gray8Property);
  9. }
  10.  
  11. public static void SetGray8(DependencyObject d, bool value)
  12. {
  13. d.SetValue(Gray8Property, value);
  14. }
  15.  
  16. private static void OnGray8Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  17. {
  18. Image currentImage = d as Image;
  19. if (currentImage == null)
  20. {
  21. return;
  22. }
  23.  
  24. bool isGray8 = (bool)d.GetValue(Gray8Property);
  25.  
  26. if (isGray8)
  27. {
  28. // 附加BitmapSourceBackup属性,备份当前BitmapSource,以备恢复用
  1. BitmapSource backupBitmapSource = (currentImage.Source as BitmapSource).CloneCurrentValue();
  2. d.SetValue(BitmapSourceBackupProperty, backupBitmapSource);
  3.  
  4. // 建立Gray8的BitmapSource
  1. FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
  2. newFormatedBitmapSource.BeginInit();
  3. newFormatedBitmapSource.Source = currentImage.Source as BitmapSource;
  4. newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray8;
  5. newFormatedBitmapSource.EndInit();
  6.  
  7. // 替换ImageSource
  1. currentImage.Source = newFormatedBitmapSource;
  2. }
  3. else
  4. {
  5. // 图像恢复操作
  1. object obj = currentImage.GetValue(BitmapSourceBackupProperty);
  2. if (obj == null)
  3. {
  4. return;
  5. }
  6.  
  7. BitmapSource bs = obj as BitmapSource;
  8. if (bs == null)
  9. {
  10. return;
  11. }
  12.  
  13. currentImage.Source = bs;
  14. }
  15. }
  16.  
  17. // 备份用源图像的附加属性,当Gray8变更时,自动附加
  1. public static readonly DependencyProperty BitmapSourceBackupProperty =
  2. DependencyProperty.RegisterAttached("BitmapSourceBackup", typeof(BitmapSource), typeof(ImageAttached),
  3. new FrameworkPropertyMetadata(null));
  4.  
  5. public static BitmapSource GetBitmapSourceBackup(DependencyObject d)
  6. {
  7. return (BitmapSource)d.GetValue(BitmapSourceBackupProperty);
  8. }
  9.  
  10. public static void SetBitmapSourceBackup(DependencyObject d, BitmapSource value)
  11. {
  12. d.SetValue(BitmapSourceBackupProperty, value);
  13. }
  14. }
  1. 然后XAML里添加 local:ImageAttached.Gray8="True"
  1.  
  1. <Image xmlns:local="clr-namespace:WpfImageGray8Sample" Source="/WpfImageGray8Sample;component/Images/44537119.jpg" local:ImageAttached.Gray8="True" />
  1.  
  1. 这样就可以方便控制Gray8了。
  1.  

  1.  
  1. Sheldon _Xiao

的回答:

  1.  
  1. 推荐了一个链接: http://www.rikware.com/post/Setting-FormatConvertedBitmap-Source-via-Binding.aspx
  1.  
  1. 里面也是用转换器实现
  1.  
  1.  

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>

  1. Public Class GrayImage
  2. Implements IValueConverter
  3. 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
  4. Try
  5. Dim newFormatedBitmapSource As New FormatConvertedBitmap
  6. newFormatedBitmapSource.BeginInit()
  7. newFormatedBitmapSource.Source = New BitmapImage(New Uri(value.ToString))
  8. newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray8
  9. newFormatedBitmapSource.EndInit()
  10. Convert = newFormatedBitmapSource
  11. Catch
  12. Convert = Nothing
  13. End Try
  14. End Function
  15. 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
  16. ConvertBack = value
  17. End Function
  18. End Class

再次的谢谢他们…

WPF 图片灰度处理的更多相关文章

  1. 制作一个简单的WPF图片浏览器

    原文:制作一个简单的WPF图片浏览器 注:本例选自MSDN样例,并略有改动.先看效果: 这里实现了以下几个功能:1.  对指定文件夹下所有JPG文件进行预览2.  对选定图片进行旋转3.  对选定图片 ...

  2. Atitit 图像处理 灰度图片 灰度化的原理与实现

    Atitit 图像处理 灰度图片 灰度化的原理与实现 24位彩色图与8位灰度图 首先要先介绍一下24位彩色图像,在一个24位彩色图像中,每个像素由三个字节表示,通常表示为RGB.通常,许多24位彩色图 ...

  3. CSS 实现图片灰度效果 兼容各种浏览器

    CSS 实现图片灰度效果 兼容各种浏览器如360浏览器 CSS实现图片灰度效果就是通过CSS样式让彩色图片呈现为灰色,相当于把一张图像的颜色模式调整为灰度,CSS可以通过以下几种方法来实现灰度效果. ...

  4. WPF 图片浏览 伪3D效果

    原文:WPF 图片浏览 伪3D效果 首先上效果图: 因项目要求,需要把图片以"好看"."炫"的效果展示出来,特地研究了一下WPF关于3D方面的制作,奈何最终成果 ...

  5. CSS 实现图片灰度效果

    非原创-从网上收索出来的文章 CSS实现图片灰度效果就是通过CSS样式让彩色图片呈现为灰色,相当于把一张图像的颜色模式调整为灰度,CSS可以通过以下几种方法来实现灰度效果. 方式1. IE滤镜 img ...

  6. WPF 图片抗锯齿,尤其是小图片更为严重

    WPF 图片抗锯齿,尤其是小图片更为严重 UseLayoutRounding="True" 搞定,就是这么给力,分享给大家

  7. WPF图片预览之移动、旋转、缩放

    原文:WPF图片预览之移动.旋转.缩放 RT,这个功能比较常见,但凡涉及到图片预览的都跑不了,在说自己的实现方式前,介绍一个好用的控件:Extended.Toolkit中的Zoombox,感兴趣的同学 ...

  8. 【转】解决WPF图片模糊最佳方法(绑定PixelWidth与PixelHeight)

    解决WPF图片模糊最佳方法(绑定PixelWidth与PixelHeight) 转载自:http://www.360doc.com/content/13/1126/09/10504424_332211 ...

  9. WPF图片浏览器(显示大图、小图等)

    原文:WPF图片浏览器(显示大图.小图等) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wangshubo1989/article/details ...

随机推荐

  1. 根据PID获取进程名&根据进程名获取PID

    Liunx中 通过进程名查找进程PID可以通过 pidof [进程名] 来查找.反过来 ,相同通过PID查找进程名则没有相关命令.在linux根目录中,有一个/proc的VFS(虚拟文件系统),系统当 ...

  2. pstack.sh 改进版

    pstack.sh 改进版本 #!/bin/bash if (( $# < 1 )) ; then echo "usage: `basename $0` pid" 1> ...

  3. 学习Numpy基础操作

    # coding:utf-8 import numpy as np from numpy.linalg import * def day1(): ''' ndarray :return: ''' ls ...

  4. ExtJS中store.findExact

    var ds = myGrid.apf_ds; var store = myGrid.getStore(); forEach(data, function (item) { if (store.fin ...

  5. 微信小程序初步运营方案

    小程序的运营方案有很多种,目前我们遇到两个事情需要解决:1.问答的内容,这块也是大家比较关心的话题.内容的定位和细节. 2.预热与推广,就这两个问题,我列出了一些自己的想法和小程序初步运营方案,有不足 ...

  6. win7注册表常用设置

    win7注册表常用设置 一.总结 一句话总结:regedit可以修改很多东西,电脑时间,背景,u盘读写,鼠标右键情况. 二.win7注册表常用设置 一. 秀出自我风格的屏幕保护画面 1.气泡屏幕保护 ...

  7. Tor (洋葱头)torbrowser

    Tor是什么 Tor是互联网上用于保护您隐私最有力的工具之一,但是时至今日仍有许多人往往认为Tor是一个终端加密工具.事实上,Tor是用来匿名浏览网页和邮件发送(并非是邮件内容加密)的.今天,我们要讨 ...

  8. Erlang 聊天室程序

    Erlang 聊天室程序( 一) Erlang 聊天室程序(二) 客户端的退出 Erlang 聊天室程序(三) 数据交换格式---json的decode Erlang 聊天室程序(四) 数据交换格式- ...

  9. 【Material Design视觉设计语言】开篇

    作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...

  10. Tricks(四十八)—— 注释一段代码

    为 if 的条件判断表达式,传一个永假的语句,来注释一段代码: # Python if False: ... ... ... # C/C++ if (false) { ... ... } 永远不要直接 ...