【发布时间】:2014-09-10 01:02:39
【问题描述】:
我正在寻找一种将位图绘制到 WPF DrawingContext 作为灰度的方法。我希望能够在给定的 x,y 位置绘制它并将其缩放到给定的宽度和高度。 256 级灰度(8 位灰度)就足够了。我在磁盘上有一个彩色位图文件,可以是 bmp、png 或 jpg 格式。
【问题讨论】:
标签: c# .net wpf graphics bitmap
我正在寻找一种将位图绘制到 WPF DrawingContext 作为灰度的方法。我希望能够在给定的 x,y 位置绘制它并将其缩放到给定的宽度和高度。 256 级灰度(8 位灰度)就足够了。我在磁盘上有一个彩色位图文件,可以是 bmp、png 或 jpg 格式。
【问题讨论】:
标签: c# .net wpf graphics bitmap
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
void DrawBitmapGreyscale(DrawingContext dc, string filename, int x, int y, int width, int height)
{
// Load the bitmap into a bitmap image object
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.UriSource = new Uri(filename);
bitmap.EndInit();
// Convert the bitmap to greyscale, and draw it.
FormatConvertedBitmap bitmapGreyscale = new FormatConvertedBitmap(bitmap, PixelFormats.Gray8, BitmapPalettes.Gray256, 0.0);
dc.DrawImage(bitmapGreyscale, new Rect(x, y, width, height));
}
【讨论】:
您可以使用效果来做到这一点 - 性能可能不如纯代码,但提供了灵活性。
<Image Source="../Images/ChartSample.png" Stretch="Uniform" Margin="5">
<Image.Effect>
<ee:ColorToneEffect DarkColor="Black" LightColor="White" ToneAmount="0" Desaturation="1" />
</Image.Effect>
你在哪里引用命名空间
xmlns:ee=”http://schemas.microsoft.com/expression/2010/effects”
【讨论】: