簡體   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