WPF设置图片灰度显示与恢复彩色显示
2012-02-07 17:19
302 查看
这是一个比较常见的需求,研究了一下,找到设置图片灰度显示和恢复彩色显示的方法,接下来,将用一个case来描述这个实现过程:窗口包含一张彩色图片和两个按钮,点击一个按钮,图片显示灰白两色,点击另外一个按钮,图片显示彩色。
窗口代码:
后台代码:
SetGrayscale是设置图片灰度显示,通过FormatConvertedBitmap类来进行转化。
SetDefalut是恢复图片彩色显示,这里图片的路径写了一些hardcode,当然,这不是重点,重点是它确实恢复了颜色。
效果图:
窗口代码:
<Window x:Class="TestForWpf.Grayscale" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Grayscale" Height="376" Width="685"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="136*" /> <RowDefinition Height="201*" /> </Grid.RowDefinitions> <Image Source="/TestForWpf;component/Images/icon.png" Name="img0" Height="120" Width="120" Margin="257,48,286,169" Grid.RowSpan="2"></Image> <Button Name="setGrayscale" Content="Set Grayscale" Width="120" Height="30" Margin="99,101,0,70" Click="setGrayscale_Click" HorizontalAlignment="Left" Grid.Row="1"></Button> <Button Name="setDefault" Content="Set Default" Height="30" Margin="414,101,129,70" Width="120" Click="setDefault_Click" Grid.Row="1" /> </Grid> </Window>
后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace TestForWpf { /// <summary> /// Interaction logic for Grayscale.xaml /// </summary> public partial class Grayscale : Window { public Grayscale() { InitializeComponent(); } private void setGrayscale_Click(object sender, RoutedEventArgs e) { SetGrayscale(img0); } private void setDefault_Click(object sender, RoutedEventArgs e) { SetDefalut(img0); } private void SetGrayscale(System.Windows.Controls.Image img) { // Set image grayscale img.IsEnabled = false; FormatConvertedBitmap bitmap = new FormatConvertedBitmap(); bitmap.BeginInit(); bitmap.Source = (BitmapSource)img.Source; bitmap.DestinationFormat = PixelFormats.Gray32Float; bitmap.EndInit(); img.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => { img.Source = bitmap; })); } private void SetDefalut(System.Windows.Controls.Image img) { img.IsEnabled = true; BitmapImage tempImg = new BitmapImage(); tempImg.BeginInit(); tempImg.UriSource = new Uri("pack://application:,,,/TestForWpf;component/Images/icon.png"); tempImg.EndInit(); img.Source = tempImg; } } }
SetGrayscale是设置图片灰度显示,通过FormatConvertedBitmap类来进行转化。
SetDefalut是恢复图片彩色显示,这里图片的路径写了一些hardcode,当然,这不是重点,重点是它确实恢复了颜色。
效果图:
相关文章推荐
- OpenCV分通道显示图片,灰度,融合,直方图,彩色直方图
- VC 6.0 MFC 读取并显示图片和彩色转灰度
- 【Android】显示彩色转灰度图片
- 自动化设置chrome禁止显示图片及恢复
- WPF设置图片显示不出来检查思路
- OpenCV分通道显示图片,灰度,融合,直方图,彩色直方图
- OpenCV分通道显示图片,灰度,融合,直方图,彩色直方图
- WPF 图片灰度处理
- iphone的icon的设置(分辨率显示的比实际图片的分辨率低)
- WPF——图片的预览,以流的方式将图片保存在数据库中,再以流的方式从数据库中读取显示图片
- ImageView设置图片显示类型
- word2010首行缩进、设置页码为第几页共几页、图片完全不显示等问题
- fpga控制vga显示彩色图片
- 如何设置图片、文字在容器(div)中垂直居中的显示?
- 设置启动图片- Iphone5 上下方黑边,页面显示不全的解决办法
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- css中设置图片的显示与隐藏
- 在WPF中实现图片一边下载一边显示
- 之前设置图片大小,总是不显示?
- wpf设计器显示图片,运行却不显示图片