Search code examples
c#system.drawing

Three System.Drawing methods manifest slow drawing or flickery: Solutions? or Other Options?


I am doing a little graphing via the System.Drawing and im having a few problems.

I'm holding data in a Queue and i'm drawing(graphing) out that data onto three picture boxes

this method fills the picture box then scrolls the graph across.

so not to draw on top of the previous drawings (and graduly looking messier) i found 2 solutions to draw the graph.

  1. Call plot.Clear(BACKGOUNDCOLOR) before the draw loop [block commented]

although this causes a flicker to appear from the time it takes to do the actual drawing loop.

  1. call plot.DrawLine(channelPen[5], j, 140, j, 0); just before each drawline [commented]

although this causes the drawing to start ok then slow down very quickly to a crawl as if a wait command had been placed before the draw command.

Here is the Code for reference:

/*plotx.Clear(BACKGOUNDCOLOR)

ploty.Clear(BACKGOUNDCOLOR)

plotz.Clear(BACKGOUNDCOLOR)*/

for (int j = 1; j < 599; j++)
            {
                if (j > RealTimeBuffer.Count - 1) break;

                QueueEntity past = RealTimeBuffer.ElementAt(j - 1);
                QueueEntity current = RealTimeBuffer.ElementAt(j);

                if (j == 1)
                {
                    //plotx.DrawLine(channelPen[5], 0, 140, 0, 0);
                    //ploty.DrawLine(channelPen[5], 0, 140, 0, 0);
                    //plotz.DrawLine(channelPen[5], 0, 140, 0, 0);
                }

                //plotx.DrawLine(channelPen[5], j, 140, j, 0);
                plotx.DrawLine(channelPen[0], j - 1, (((past.accdata.X - 0x7FFF) / 256) + 64), j, (((current.accdata.X - 0x7FFF) / 256) + 64));

                //ploty.DrawLine(channelPen[5], j, 140, j, 0);
                ploty.DrawLine(channelPen[1], j - 1, (((past.accdata.Y - 0x7FFF) / 256) + 64), j, (((current.accdata.Y - 0x7FFF) / 256) + 64));

                //plotz.DrawLine(markerPen, j, 140, j, 0);
                plotz.DrawLine(channelPen[2], j - 1, (((past.accdata.Z - 0x7FFF) / 256) + 94), j, (((current.accdata.Z - 0x7FFF) / 256) + 94));


            }

Is there any tricks to avoid these overheads?

If not would there be any other/better solutions?

EDIT: See [Final Solution] below for solution code


Solution

  • PictureBox already has double-buffering turned on. The only way you could perceive flicker is when you draw directly to the screen instead of using the Paint event. It isn't clear whether you do from your snippet. Buffering yourself with a bitmap would work too but isn't as efficient as the double-buffering implemented by Windows Forms.

    Call its Invalidate() method when the data changes, do the drawing in the Paint event (using e.Graphics) and it won't flicker.