Layers with GDI +
I'm going to create a paint program with layers and use GDI + to display them. I want to use GDI + because it supports transparency.
The point is that drawing lines in DC is very fast, but drawing directly to a bitmap is very slow. It only happens quickly if you lock the bits and start adjusting the pixels. Can I draw multiple DC instances on my WM_PAINT event and then just draw a DrawBitmap for each layer in MemDC? What's the best way to do this?
thanks
a source to share
GDI + is of course fast enough for a paint program. I am using it (from C #) for high speed animation (> 30fps).
It seems that you want to be able to manipulate individual pixels. It's very fast with LockBits, and while it's a little awkward to use in C # (requiring a pointer and a tag unsafe
), it looks like it wouldn't be that hard in C ++.
You probably don't want to copy from multiple layers directly to the reference surface inside a paint event. Instead, this rendering should be done to the offscreen buffer (B1). After B1 is drawn when all copy / paint operations are complete, copy it to a second offscreen buffer (B2) and then invalidate / update the reference surface. In a control paint event, you copy from B2 to the visible surface.
You don't want to paint directly on the visible surface with a multi-step drawing operation as a form of flickering occurs (sometimes the screen will redraw while your code is only partially through a multi-step operation, so the user sees a random half of the finished frame).
You can display one buffer off-screen and copy it from the visible surface to a paint event. The main complication here is that you have to deal with somehow "wandering" paint events, that is, events that are not caused by the intentional invalidation of the control, but something else (for example, the user drags another window over yours) ... If you copy from an offscreen buffer to a surface and the buffer is only halfway, you get flickering. If you block the paint event before the buffer has finished drawing, you will see "trace shapes" on your element, which looks even worse.
The solution is the double buffer approach described above. Wandering (or non-wandering when you're invalid) paint events are copied from B2, which is always fully rendered and refreshed so it doesn't flicker. Double buffering uses more memory, but in a paint program that has multiple levels, it doesn't really matter.
a source to share