How can I prevent GDI + from redrawing everything on paint?

I want to change the color of some parts of a control that uses GDI + for drawing, for example when some objects are hovered / clicked, for example.

How can I re-draw only the parts I need? It seems unfortunate to re-draw thousands of pixels when I only need to make changes to some of them.

To be specific, I have drawn anywhere from 1 to 128 rectangles, and I will need to re-draw 1 to 128 rectangles in different events. I don't want to re-draw 128 rectangles to only make changes in 1-127.

I've read that it's a good idea to use paint code in places that are not the paint event. But is it possible to do this by relying on the paint event?


source to share

1 answer

You can use one of the methods Control.Invalidate

to specify the region you want to redraw.
Then, in the Paint event handler, you can check for an invalid region with the property e.ClipRectangle




All Articles