WPF 图片灰度处理
原文: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
再次的谢谢他们…
WPF 图片灰度处理的更多相关文章
- 制作一个简单的WPF图片浏览器
原文:制作一个简单的WPF图片浏览器 注:本例选自MSDN样例,并略有改动.先看效果: 这里实现了以下几个功能:1. 对指定文件夹下所有JPG文件进行预览2. 对选定图片进行旋转3. 对选定图片 ...
- Atitit 图像处理 灰度图片 灰度化的原理与实现
Atitit 图像处理 灰度图片 灰度化的原理与实现 24位彩色图与8位灰度图 首先要先介绍一下24位彩色图像,在一个24位彩色图像中,每个像素由三个字节表示,通常表示为RGB.通常,许多24位彩色图 ...
- CSS 实现图片灰度效果 兼容各种浏览器
CSS 实现图片灰度效果 兼容各种浏览器如360浏览器 CSS实现图片灰度效果就是通过CSS样式让彩色图片呈现为灰色,相当于把一张图像的颜色模式调整为灰度,CSS可以通过以下几种方法来实现灰度效果. ...
- WPF 图片浏览 伪3D效果
原文:WPF 图片浏览 伪3D效果 首先上效果图: 因项目要求,需要把图片以"好看"."炫"的效果展示出来,特地研究了一下WPF关于3D方面的制作,奈何最终成果 ...
- CSS 实现图片灰度效果
非原创-从网上收索出来的文章 CSS实现图片灰度效果就是通过CSS样式让彩色图片呈现为灰色,相当于把一张图像的颜色模式调整为灰度,CSS可以通过以下几种方法来实现灰度效果. 方式1. IE滤镜 img ...
- WPF 图片抗锯齿,尤其是小图片更为严重
WPF 图片抗锯齿,尤其是小图片更为严重 UseLayoutRounding="True" 搞定,就是这么给力,分享给大家
- WPF图片预览之移动、旋转、缩放
原文:WPF图片预览之移动.旋转.缩放 RT,这个功能比较常见,但凡涉及到图片预览的都跑不了,在说自己的实现方式前,介绍一个好用的控件:Extended.Toolkit中的Zoombox,感兴趣的同学 ...
- 【转】解决WPF图片模糊最佳方法(绑定PixelWidth与PixelHeight)
解决WPF图片模糊最佳方法(绑定PixelWidth与PixelHeight) 转载自:http://www.360doc.com/content/13/1126/09/10504424_332211 ...
- WPF图片浏览器(显示大图、小图等)
原文:WPF图片浏览器(显示大图.小图等) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wangshubo1989/article/details ...
随机推荐
- tp5 thinkphp5 index.php隐藏 iis 重写 伪静态
面临的问题如下: 网上找了个源码,tp5的,公司服务器是iis,源码是隐藏index.php使用了路由,iis默认去找那个路径的文件了,找不到,所以报错了 如果没有iis没有安装"url重写 ...
- thinkphp5 tp5 获取模块名控制器名方法名
<?php namespace app\index\controller; use think\Db; use think\Controller; class Base extends Cont ...
- 学习Numpy
1.什么是numpy NumPy系统是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多( ...
- sysbench压测Oracle
安装: yum -y install make m4 autoconf automake libtool pkgconfig libaio-devel rpm -Uvh http://dl.fedo ...
- 编译Android下可用的FFmpeg+x264
编译Android下可用的FFmpeg+x264 编译x264: 下载最新版的x264 ftp://ftp.videolan.org/pub/videolan/x264/snapshots/ 1.解压 ...
- 检测dll是32/64位?(直接读dll文件包含的某几个字节进行判断)
检查dll是32位还是64位? #include "stdafx.h" #include <Windows.h> int _tmain(int argc, _TCHAR ...
- [Angular] Subscribing to the valueChanges Observable
For example we have built a form: form = this.fb.group({ store: this.fb.group({ branch: '', code: '' ...
- 注册表 Run、RunOnce 浅析
绝大多数使用过 Windows 操作系统的用户都不会对注册表的 Run.RunOnce 键值感到陌生,但你真的了解所有这些键值的细节吗?让我们具体说来. 本文在Win2000,WinXp.Vista. ...
- [Angular] Create a simple *ngFor
In this post, we are going to create our own structure directive *ngFor. What it should looks like i ...
- matlab 机器学习相关函数、api
matlab 对数据集的默认组织方式是,X∈Rd×N d:行数,表示特征向量的长度: N:列数,表示样本的数目: 1. 模型.预测.mse % 加载 matlab 内置数据到内存 X = abalon ...