Search code examples
c#.netgraphicsonpaint

Efficient use of OnPaint


I am programming in Visual Studio .Net and using C#.

I am creating my own control that draws a wave based on values I get from an analog to digital converter (ADC). I take the incoming points and convert them into X and Y points to properly draw the graph in my control.

I have a loop inside my OnPaint method that goes through all the points and calls the DrawLine method between the current point and the next point.

However, this is very inefficient as some of these graphs have 8192 points and the system actually has nine ADCs that I would like to show at the same time. Every time the page redraws it takes almost a second for all graphs to redraw (especially during debug).

On top of that, I have functionality that allows you to zoom in and pan across the waves to get a better view (acts a lot like google maps does) and all 9 waves zoom in and pan together.

All of this functionality is very "jerky" because I am calling invalidate on mousewheel and mousemove. Basically, it all works but not as smoothly as I would like.

I was wondering if there were a way to create a predrawn object from the data and then just draw a dilated and translated version of the picture in the draw area.

Any help would be greatly appreciated even if it is just pointing me in the right direction.


Solution

  • Create a Bitmap object, and draw to that.

    In your Paint handler, just blit the Bitmap to the screen.

    That will allow you decouple changing the scale, from re-rendering the data.