I have a WinForms control on which I want to display two things:
- An underlying image painstakingly loaded bit-by-bit from an external input device; and
- A series of DrawLine calls that create a visual pattern over that image.
Thing #1, for our purposes, doesn’t change, and I’d rather not have to redraw it.
Thing #2 has to be redrawn relatively quickly, as it rotates when the user turns another control.
In my fantasy, I want to put each Thing in its own Graphics object, give #2 a transparent background, and simply hit #2 with a rotational transformation to match the user control setting. But I don’t see a way to make a Graphics object transparent, nor a way to rotate what’s already been drawn on one. So I’m probably asking Graphics to do something it wasn’t designed for.
Here’s my question: What’s the best way to set this up? Should I attempt to overlap my Graphics objects, or is there some completely different and better way to do this that I’m not thinking of?
The Windows painting model is a good match for your requirements. It separates drawing the background (OnPaintBackground) from the foreground (OnPaint). That however doesn’t mean that you can only paint the background once and be done with it. Window surface invalidation invokes both. This is above all required to make anti-aliasing effects work properly, they can only look good against a known background color.
Punt this and draw the Image in the OnPaintBackground() override. You can let Control do this automatically for you by assigning the BackgroundImage property. You’ll probably need to set the DoubleBuffer property to true to avoid the flicker you’ll see when the background is drawn, temporarily wiping out the foreground pixels. Call Invalidate() if you need to update the foreground.
To be complete, your fantasy is in fact possible. You’d need to overlay the image with a toplevel layered window. That is easy to get with a Form whose TransparencyKey property is set. Here is a sample implementation:
One interesting artifact: minimizing the form and restoring it again looks, erm, special.