2013-04-04 48 views
2

我使用TransformedBitmap类使用TransformedBitmap.CopyPixels将缩放图像绘制到Bitmap。有没有办法指定使用的缩放模式? RenderOptions.SetBitmapScalingMode似乎没有影响任何东西。我想使用最近的邻居,但它似乎使用某种双线性滤波器。TransformedBitmap缩放模式

回答

3
  • 不可能指定缩放算法,它是通过设计。
  • RenderOptions.SetBitmapScalingMode仅适用于渲染,例如,你有一个32 * 32的图标,并希望在256 * 256,但仍处于块状方式(近邻)

更新,以显示它

你是怎么克服这个问题的一些方法:

自己做这件事: 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继承的,很可能根本不会改变现有的代码。

+0

有趣拿了,你有一个替代有什么建议? (鉴于我有一个bitmapsource需要缩放和复制到位图) – phosphoer 2013-04-05 18:40:10

+0

我已经在我的答案中添加了几种方法给你。 (抱歉,但我在我的手机上,这不是很实际) – Aybe 2013-04-05 23:42:33

+0

我已经更新了我的答案。 – Aybe 2013-04-09 17:14:27

0
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