繁体   English   中英

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

[英]WPF - WritablebitmapEx render a smooth line

我需要使用WritableBitmap渲染一条“平滑”的线'我正在使用WritableBitmapExtenstions

每12毫秒,我得到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;      
   }  

结果 :

在此输入图像描述

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

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

你可以看到我想达到的那种平滑。

尝试使用DrawLineAa (Aa == Antialiased)扩展方法而不是DrawLine

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM