My Application is recieving data from external hardware and plots it onto a panel (can be actually any other child of "Control"). The painting currently happens in a "OnPaint" callback. A List is used to store the recently recieved data to allow to redraw the whole graph in OnPaint to get the proportions right if e.g. the window gets resized.
The graph itself is drawn with the e->Graphics
element using lines between two data points.
This is working fine, but when I have new data coming in every 50 ms (= repaint the whole graph), the graph soon begins to flicker. The flickering gets stronger the more data needs to be plotted (when the right side of the control is reached, the data cache gets cleared, so there is a finite max number of data points in the graph).
The main part of my code:
void Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
{
Size^ s = m_Control->Size;
Bitmap^ bmp = gcnew Bitmap(s->Width, s->Height);
Graphics^ g = Graphics::FromImage(bmp);
for each(double y in m_Data)
{
/* ...calculations etc... */
g->DrawLine(Pens::Blue, recentX, recentY, currentX, currentY);
}
e->Graphics->DrawImageUnscaled(bmp, 0, 0);
}
Any suggestion how I can optimize the painting to get rid of the flickering? Thanks in advance!