WPF 图片灰度处理
2011-12-12 16:46
274 查看
文章的内容是来自微软中文技术论坛的一个帖子,当时是想将一段将图片灰度处理的代码转换为XAML的一个样式,在这里要谢谢
XiaoYanQiang、Sheldon_Xiao、shixin的热情回答,现在将他们的回答贴出来供大家学习参考.内容如下:
提问:这个功能如何写成一个样式,将一个窗体内所有的Image控件的图片格式都转换为Gray8
BitmapImagebitmapImage=newBitmapImage(newUri("D:\\Face.jpg"));
FormatConvertedBitmapnewFormatedBitmapSource=newFormatConvertedBitmap();
newFormatedBitmapSource.BeginInit();
newFormatedBitmapSource.Source=bitmapImage;
newFormatedBitmapSource.DestinationFormat=PixelFormats.Gray8;
newFormatedBitmapSource.EndInit();
img.Source=newFormatedBitmapSource;
this.Content=img;
XiaoYanQiang的回答:
的回答:
shixin的回答:
<Windowx: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>
<ImageSource="{BindingRelativeSource={RelativeSourceSelf},Path=Tag,Converter={StaticResourceGrayImage}}"Tag="C:\Test.jpg"/>
</Grid>
</Window>
xaml部分还可以写成这样
<Window.Resources>
<y:GrayImagex:Key="GrayImage"/>
<StyleTargetType="{x:TypeImage}">
<SetterProperty="Source"Value="{BindingRelativeSource={RelativeSourceSelf},Path=Tag,Converter={StaticResourceGrayImage}}"/>
</Style>
</Window.Resources>
<Grid>
<ImageTag="C:\Test.jpg"/>
</Grid>
再次的谢谢他们…
XiaoYanQiang、
提问:这个功能如何写成一个样式,将一个窗体内所有的Image控件的图片格式都转换为Gray8
BitmapImagebitmapImage=newBitmapImage(newUri("D:\\Face.jpg"));
FormatConvertedBitmapnewFormatedBitmapSource=newFormatConvertedBitmap();
newFormatedBitmapSource.BeginInit();
newFormatedBitmapSource.Source=bitmapImage;
newFormatedBitmapSource.DestinationFormat=PixelFormats.Gray8;
newFormatedBitmapSource.EndInit();
img.Source=newFormatedBitmapSource;
this.Content=img;
XiaoYanQiang的回答:
publicclassImageAttached { //Gray8附加属性,Gary8图片样式的"开关"
publicstaticreadonlyDependencyPropertyGray8Property= DependencyProperty.RegisterAttached("Gray8",typeof(bool),typeof(ImageAttached), newFrameworkPropertyMetadata((bool)false, newPropertyChangedCallback(OnGray8Changed))); publicstaticboolGetGray8(DependencyObjectd) { return(bool)d.GetValue(Gray8Property); } publicstaticvoidSetGray8(DependencyObjectd,boolvalue) { d.SetValue(Gray8Property,value); } privatestaticvoidOnGray8Changed(DependencyObjectd,DependencyPropertyChangedEventArgse) { ImagecurrentImage=dasImage; if(currentImage==null) { return; } boolisGray8=(bool)d.GetValue(Gray8Property); if(isGray8) { //附加BitmapSourceBackup属性,备份当前BitmapSource,以备恢复用
BitmapSourcebackupBitmapSource=(currentImage.SourceasBitmapSource).CloneCurrentValue(); d.SetValue(BitmapSourceBackupProperty,backupBitmapSource); //建立Gray8的BitmapSource
FormatConvertedBitmapnewFormatedBitmapSource=newFormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source=currentImage.SourceasBitmapSource; newFormatedBitmapSource.DestinationFormat=PixelFormats.Gray8; newFormatedBitmapSource.EndInit(); //替换ImageSource
currentImage.Source=newFormatedBitmapSource; } else { //图像恢复操作
objectobj=currentImage.GetValue(BitmapSourceBackupProperty); if(obj==null) { return; } BitmapSourcebs=objasBitmapSource; if(bs==null) { return; } currentImage.Source=bs; } } //备份用源图像的附加属性,当Gray8变更时,自动附加
publicstaticreadonlyDependencyPropertyBitmapSourceBackupProperty= DependencyProperty.RegisterAttached("BitmapSourceBackup",typeof(BitmapSource),typeof(ImageAttached), newFrameworkPropertyMetadata(null)); publicstaticBitmapSourceGetBitmapSourceBackup(DependencyObjectd) { return(BitmapSource)d.GetValue(BitmapSourceBackupProperty); } publicstaticvoidSetBitmapSourceBackup(DependencyObjectd,BitmapSourcevalue) { d.SetValue(BitmapSourceBackupProperty,value); } }
然后XAML里添加local:ImageAttached.Gray8="True"
<Imagexmlns: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
里面也是用转换器实现
<Windowx: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>
<ImageSource="{BindingRelativeSource={RelativeSourceSelf},Path=Tag,Converter={StaticResourceGrayImage}}"Tag="C:\Test.jpg"/>
</Grid>
</Window>
xaml部分还可以写成这样
<Window.Resources>
<y:GrayImagex:Key="GrayImage"/>
<StyleTargetType="{x:TypeImage}">
<SetterProperty="Source"Value="{BindingRelativeSource={RelativeSourceSelf},Path=Tag,Converter={StaticResourceGrayImage}}"/>
</Style>
</Window.Resources>
<Grid>
<ImageTag="C:\Test.jpg"/>
</Grid>
PublicClassGrayImage
ImplementsIValueConverter
PublicFunctionConvert(ByValvalueAsObject,ByValtargetTypeAsType,ByValparameterAsObject,ByValcultureAsGlobalization.CultureInfo)AsObjectImplementsIValueConverter.Convert
Try
DimnewFormatedBitmapSourceAsNewFormatConvertedBitmap
newFormatedBitmapSource.BeginInit()
newFormatedBitmapSource.Source=NewBitmapImage(NewUri(value.ToString))
newFormatedBitmapSource.DestinationFormat=PixelFormats.Gray8
newFormatedBitmapSource.EndInit()
Convert=newFormatedBitmapSource
Catch
Convert=Nothing
EndTry
EndFunction
PublicFunctionConvertBack(ByValvalueAsObject,ByValtargetTypeAsType,ByValparameterAsObject,ByValcultureAsGlobalization.CultureInfo)AsObjectImplementsIValueConverter.ConvertBack
ConvertBack=value
EndFunction
EndClass
再次的谢谢他们…
相关文章推荐
- WPF 图片灰度处理
- WPF 将图片进行灰度处理
- WPF 将图片进行灰度处理
- Unity教程之-Unity3d中图片的灰度处理
- 彩色图片的各种处理方法:灰度,黑白,底片效果
- Android关于图片处理,更改图片灰度
- WPF中使用BitmapImage处理图片文件 .
- WPF中使用BitmapImage处理图片文件
- Qt creator5.7 OpenCV249之图片灰度处理(含源码下载)
- [Python]图像处理日记——第一天(如何查询图片属性,数组与RGB,灰度图像如何互换)
- winform对图片进行灰度处理
- Canvas图片灰度等相关处理
- 图片灰度处理
- iPhone How-to:图片的灰度处理
- C#图片灰度处理(位深度24→位深度8)
- 【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇三:批量处理后的txt文件入库处理
- 【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇三:批量处理后的txt文件入库处理
- iOS学习笔记--图片灰度处理
- 易语言-图片灰度处理
- 【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇四:关于OneNote入库处理以及审核