Nick
Nick

Reputation: 10499

Draw stroke using Windows Form Graphics - performance improvement needed

I have a Panel where I need to draw some Strokes using my mouse. My Strokes are simply lists of Points and I add them using mouse events. Following is my code:

private void Panel_MouseDown(object sender, MouseEventArgs e)
{
    isDrawing = true;
    currStroke = new Stroke(new Pen(Color.Black, 2));
    currStroke.Points.Add(e.Location);
    visuals.Add(currStroke);
}

private void Panel_MouseMove(object sender, MouseEventArgs e)
{
    if (isDrawing)
    {
        currStroke.Points.Add(e.Location);
        Invalidate(false);
    }
}

private void Panel_MouseUp(object sender, MouseEventArgs e)
{
    isDrawing = false;
}

I overrided OnPaint method where I draw all my visuals, and in particoluar to draw stroke I use this code:

e.Graphics.DrawLines(stroke.Pen, stroke.Points.ToArray());

It works, but I when I'm drawing my Panel blinks, that is I can see my panel appear and disappear quickly. It is probably due to the continuous calls to the Invalidate() method.

Is there any workaround in order to improve perfomance and remove the blink effect?

Upvotes: 2

Views: 581

Answers (1)

nvoigt
nvoigt

Reputation: 77354

You should set the DoubleBuffered property of your control to true.

From the link:

Buffered graphics can reduce or eliminate flicker that is caused by progressive redrawing of parts of a displayed surface. Buffered graphics require that the updated graphics data is first written to a buffer. The data in the graphics buffer is then quickly written to displayed surface memory. The relatively quick switch of the displayed graphics memory typically reduces the flicker that can otherwise occur.

Upvotes: 2

Related Questions