2013-04-04 90 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