原文:
WPF 将图片进行灰度处理
这个功能使用使用了 FormatConvertedBitmap(为BitmapSource提供像素格式转换功能)
代码如下:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Image img = new Image();
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;
}
}