WPF - WritablebitmapEx渲染一条平滑的线条

时间:2014-02-20 13:08:59

标签: c# wpf rendering writablebitmap

我需要使用WritableBitmap'使用WritableBitmapExtenstions呈现“平滑”线。

每12 ms我得到12个由(X,Y)组成的点,其中Y被标准化为屏幕的中心,X表示图像表面上的像素(位图)。

初始化:

 _wb =  new WriteableBitmap((int)mainGrid.ActualWidth, (int)mainGrid.ActualHeight, 96.0, 96.0, PixelFormats.Pbgra32, null);
 image.Source = _wb;

 CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);

CompositionTarget_Rendering:

   Point [] points = null;
   if (blocks.TryDequeue(out points)) // blocks is a ConcurrentQueue which gets enqueued on a timer interval on another thread.
   {
       using (_wb.GetBitmapContext())
       {                    
           Draw(points);
       }
   }

画画:

   private void Draw(Point[] points)
   {
        int x1, y1, x2, y2;

        if (lastX != 0 && lastY != 0)
        { // Draw connection to last line segment.
            x1 = lastX;
            y1 = lastY;
            x2 = (int)points[0].X;
            y2 = (int)points[0].Y;

            _wb.DrawLine(x1, y1, x2, y2, Colors.Red);
        }

        for (int i = 0; i < points.Count() - 1; i++)
        {// draw lines. [0],[0] - [1],[1]  ; [1],[1] - [2],[2]   .....and so on.
            x1 = (int)points[i].X;
            y1 = (int)points[i].Y;
            x2 = (int)points[i + 1].X;
            y2 = (int)points[i + 1].Y;

            _wb.DrawLine(x1, y1, x2, y2, Colors.Red);
        }

        lastX = (int)points[points.Count() - 1].X;
        lastY = (int)points[points.Count() - 1].Y;      
   }  

结果:

enter image description here

线条完全就位,但绘制的方式并不顺畅,即使很难,我使用了Writablebitmap并绘制了渲染事件中的所有线条,每个线段仍然作为批处理呈现。

总而言之,我应该一次绘制一个像素以使其平滑? 如果你看起来像WritablebitmapEx曲线样本名为“WriteableBitmapExCurveSample.Wpf”的项目 (这将要求您从上面的链接下载样本)

你可以看到我想要达到的平滑度。

2 个答案:

答案 0 :(得分:2)

尝试使用DrawLineAa(Aa == Antialiased)扩展方法代替DrawLine

答案 1 :(得分:2)

为了获得最合适的结果,您可以应用post-fx矩阵滤波器。 http://lodev.org/cgtutor/filtering.html

我不记得如何使用asp.net实现这一点,但我认为在CompositionTarget中有类似的东西。