【发布时间】:2013-03-27 03:02:14
【问题描述】:
我正在使用TransformedBitmap 类将缩放图像绘制到使用TransformedBitmap.CopyPixels 的Bitmap。有没有办法指定使用的缩放模式? RenderOptions.SetBitmapScalingMode 似乎没有任何影响。我想使用最近邻,但它似乎使用某种双线性过滤器。
【问题讨论】:
我正在使用TransformedBitmap 类将缩放图像绘制到使用TransformedBitmap.CopyPixels 的Bitmap。有没有办法指定使用的缩放模式? RenderOptions.SetBitmapScalingMode 似乎没有任何影响。我想使用最近邻,但它似乎使用某种双线性过滤器。
【问题讨论】:
public static class Extensions
{
public static BitmapFrame Resize(this
BitmapSource photo, int width, int height,
BitmapScalingMode scalingMode)
{
var group = new DrawingGroup();
RenderOptions.SetBitmapScalingMode(
group, scalingMode);
group.Children.Add(
new ImageDrawing(photo,
new Rect(0, 0, width, height)));
var targetVisual = new DrawingVisual();
var targetContext = targetVisual.RenderOpen();
targetContext.DrawDrawing(group);
var target = new RenderTargetBitmap(
width, height, 96, 96, PixelFormats.Default);
targetContext.Close();
target.Render(targetVisual);
var targetFrame = BitmapFrame.Create(target);
return targetFrame;
}
}
取自http://weblogs.asp.net/bleroy/resizing-images-from-the-server-using-wpf-wic-instead-of-gdi
【讨论】:
更新
解决此问题的几种方法:
自己动手: http://tech-algorithm.com/articles/nearest-neighbor-image-scaling/
使用表格: https://stackoverflow.com/a/1856362/361899
自定义绘图: How to specify the image scaling algorithm used by a WPF Image?
也有 AForge,但这可能对您的需求来说有点过头了。
更新 2
WriteableBitmapEx 可能会为您轻松完成这项工作:http://writeablebitmapex.codeplex.com/
您可以调整 WriteableBitmap 的大小,指定插值模式,并有最近邻。
TransformedBitmap 和 WriteableBitmapEx 都继承自 BitmapSource,您可能根本不需要对现有代码进行任何更改。
【讨论】: