Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I searched the site but didn't find anything.
I have 1000 polygons drawn on OnPaint() and some other stuff. Is there a way to speed up the drawing, by not drawing the polygons every time (but still see them of course) and only "refresh" the other stuff?

I also use double buffering to overcome the flickering.
Posted

You certainly need to use double buffering. The only one method to accelerate things is to calculate some subset of the points of your window area, only those really touched by your most recent change in data, and call InvalidateRect or InvalidateRegion. You can combine any number of different region, they will be ORed together and the Windows message handling mechanism (very special for WM_PAINT) will, to some extent, optimize out redundant redrawing. Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd145002%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/aa769105%28v=vs.85%29.ASPX[^].

However, this measure may have limited effect if the invalidate areas are not much smaller then the whole scene.

By they way, you should not use anything like "refresh", you should only use "invalidate" methods. If you did something except invalidation, it could also affect your performance badly.

If my advice does not help much, you should critically review your code and optimize it. A good code profiler could help. If this is not enough, perhaps your graphics is too rich and your performance requirements are too tough, for the relatively slow GDI+. Then you should think about using DirectX instead:
http://en.wikipedia.org/wiki/DirectX[^],
http://www.dmoz.org/Computers/Programming/Libraries/DirectX/[^],
http://www.microsoft.com/en-us/download/details.aspx?id=6812[^],
http://msdn.microsoft.com/en-us/library/windows/apps/jj554502.aspx[^].

See also: http://msdn.microsoft.com/library/windows/apps/hh452744.aspx[^].

—SA
 
Share this answer
 
v2
Comments
xumepoc 17-Nov-13 14:52pm    
Thanks for the reply,
yes by refresh I do mean invalidate, but I need to do that every second or mouse move/action.

I have tried Direct2D, but for some reason it is too slow, many times slower then pure GDI.
Sergey Alexandrovich Kryukov 17-Nov-13 15:03pm    
Invalidate? — very good.

But "every second" is a big mistake. You need to do invalidate only when your data to be rendered is actually changed, and, unfortunately, if you need to pick up mouse motion (say, put it into a line), it could be much faster then 1 sec. :-) But the graphic editor following mouse events is quite possible. You could have some other performance leaks, or your graphic model is overwhelmed. I would rather suspect your code.

By the way, the best way to correctly develop implementation part is to embed it into the data model. The data model ultimately "knows" when it is updated, so it should trigger proper invalidation, throw some dependency injection or event mechanism.

—SA
If the polygons don't change very often I would suggest using a bitmap to store the image then you can use a separate thread to update the image when the polygons change and only display the bitmap in the on paint.

This is basically a method for doing your own double buffering.

(posted from my mobile phone)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Nov-13 15:04pm    
Effectively, this is already done, by double buffering, as OP mentioned. The different story if it was implemented incorrectly or poorly, but we don't know...

I listed some measures to be taken, but suspect OP's code is a performance leak, please see last comments to my answer...

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900