简体   繁体   中英

WPF - WritablebitmapEx render a smooth line

I need to render a "smooth" line using a WritableBitmap' i'm using WritableBitmapExtenstions .

Every 12 ms i get 12 points consisting of an (X,Y) where the Y is normalized to the center of the screen, and the X represents a pixel on the the image surface (Bitmap) .

Init :

 _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);
       }
   }

Draw :

   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;      
   }  

The result :

在此输入图像描述

Well the lines are exactly in place , but the way it was drawn was not smooth even tough i used a Writablebitmap and drew all the lines on the Rendering event , each segment was still rendered as a batch.

so to conclude should i draw one pixel at a time in order to make this smooth ? if you look as the WritablebitmapEx Curve sample the project named "WriteableBitmapExCurveSample.Wpf" (This will require you to download the samples from the link above)

you can see the kind of smoothness i wan't to achieve .

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

For the most proper result, you could apply a post-fx matrix filter. http://lodev.org/cgtutor/filtering.html

I don't remember well how to implement this with asp.net but i think there is something similar in CompositionTarget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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