Optimize drawing for paint application c#

佐手、 提交于 2019-12-23 01:58:12

问题


I am making a paint project like photoshop using c#. I have used GDI+ for the drawing. Sadly I cannot post the screen shot cuz of the reputation points required. EDIT : Ok i got enough rep to upload a pic.

My drawing using the mouse lags when the brush size increases. I have a canvasBuffer which is drawn to the canvas panel

protected override void OnPaint(PaintEventArgs e)
    {
        if (canvasBuffer != null)
        {
            using (Graphics g = e.Graphics)
            {
                g.DrawImage(canvasBuffer, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel);
            }
        }
    }

And this is what happens when any painting is done.

  1. I store the list of points of the mouse drag event.
  2. I draw a series of circle from point a to point b with the recorded point as its center to draw a smooth line
  3. This drawing is done on the bitmap stored in the list of strokes in the layer class. This drawing is also done with CompositingMode.SourceCopy to implement alpha value drawings
  4. I have a layerBuffer that stores the final image of the layer. I draw the changes affected by the stroke to this layerBuffer by clearing it with a drawing of a transparent color using SourceCopy Compatibility mode and then draw the bitmaps in the list of strokes using SourceOver
  5. Due to the layering system I am implementing, I draw all the layer buffer to a pictureBuffer. This picture Buffer is finally drawn to the canvasBuffer with scaling transformations.

Note : The affected area of the canvas Buffer is done in the same manner as the layer Buffer i.e by clearing the affected part and redrawing the entire affected part of the picture buffer back again. If I do not clear the previous drawing, drawing with alpha value does not work as expected.

Please help me to optimize this code or suggest some new ways to improve the performance and reduce the lag while drawing using the mouse. Also, would separating the drawing code and the calculation of the points and drawing of the buffers using threads help ?

Here is the code.

public void PSF_Painted(PSF_PaintEvent e)
    {
        Layer SelectedLayer = psf.Layers[0];//Get selected layer here
        if ((BrushTool)getActiveTool() != null)
        {
            //getting the pen attributes from the brush tool
            BrushTool brushTool = (BrushTool)getActiveTool();
            Pen pen = brushTool.getPen();
            Brush brush = pen.Brush;
            int brushSize = (int)pen.Width;
            //loading points data
            List<Point> recordedPoints = null;
            Point currentPoint = new Point(0, 0);
            Point previousPoint = new Point(0, 0);
            if (e.RecordedPoints != null)
            {
                recordedPoints = e.RecordedPoints;
                if (recordedPoints.Count > 1)
                {
                    currentPoint = recordedPoints[recordedPoints.Count - 1];
                    previousPoint = recordedPoints[recordedPoints.Count - 2];
                }
                else if (recordedPoints.Count == 1)
                {
                    currentPoint = recordedPoints[0];
                    previousPoint = currentPoint;
                }
            }
            if (e.PaintEventType == PSF_PaintEvent.StrokeStarted)
            {
                //Console.WriteLine("StrokeStarted");
                SelectedLayer.Strokes.Add(new Bitmap(SelectedLayer.Width, SelectedLayer.Height));
            }
            else if (e.PaintEventType == PSF_PaintEvent.Painting)
            {
                //Draw the drawing in the bitmap of the layer's stroke data
                using (Graphics g = Graphics.FromImage(SelectedLayer.Strokes[SelectedLayer.Strokes.Count - 1]))
                {
                    List<Point> points = Global.GetPointsOnLine(previousPoint.X, previousPoint.Y, currentPoint.X, currentPoint.Y);
                    for (int i = 0; i < points.Count; i++)
                    {
                        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                        if (pen.Width == 1)
                        {
                            g.FillRectangle(brush, new Rectangle(points[i].X, points[i].Y, brushSize, brushSize));
                        }
                        else
                        {
                            g.FillEllipse(brush, new Rectangle(points[i].X, points[i].Y, brushSize, brushSize));
                        }
                        int xt, xb, yt, yb;
                        xt = points[i].X;
                        xb = points[i].X + brushSize;
                        yt = points[i].Y;
                        yb = points[i].Y + brushSize;

                        if (xt < 0) xt = 0;
                        if (xb > psf.Width) xb = SelectedLayer.Width;
                        if (yt < 0) yt = 0;
                        if (yb > psf.Height) yb = SelectedLayer.Height;

                        //Refresh changes to the affected part of the canvas buffer
                        Rectangle affectedRect = new Rectangle(xt, yt, xb - xt, yb - yt);
                        float zf = psf.ZoomFactor;
                        Rectangle canvasAffectedRect = new Rectangle((int)(affectedRect.X * zf), (int)(affectedRect.Y * zf), (int)(affectedRect.Size.Width * zf), (int)(affectedRect.Size.Height * zf));
                        SelectedLayer.invalidateLayerBuffer(affectedRect);
                        invalidateCanvasBuffer(canvasAffectedRect);
                    }
                }
            }
            else if (e.PaintEventType == PSF_PaintEvent.StrokeCompleted)
            {
                //Console.WriteLine("StrokeCompleted");
            }
        }
        this.Invalidate();
    }

回答1:


Ok, I found a solution for this optimization. I only recorded the points which were at a distance > 1/5th the size of the brush. I realized that I did not need a very perfect line. Therefore the compromise in the quality of the line for performance can be traded.



来源:https://stackoverflow.com/questions/10126862/optimize-drawing-for-paint-application-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!