【问题标题】:TransformedBitmap Scaling ModeTransformedBitmap 缩放模式
【发布时间】:2013-03-27 03:02:14
【问题描述】:

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

【问题讨论】:

    标签: c# wpf image scaling


    【解决方案1】:
     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

    【讨论】:

      【解决方案2】:
      • 无法指定缩放算法,这是设计使然。
      • 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,您可能根本不需要对现有代码进行任何更改。

      【讨论】:

      • 有趣,你有什么替代方案的建议吗? (鉴于我有一个需要缩放并复制到位图的位图源)
      • 我在回答中为您添加了一些方法。 (抱歉,我在用手机,不太实用)
      • WriteableBitmapEx 看起来很适合使用,谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-28
      相关资源
      最近更新 更多