C# WinForms Drawing without viewing
A friend of nine gave me an idea for buffering: to view the drawing after the drawing process is completed. He told me to draw on a graphic (a panel in this case) which is invisible for the user. But when the drawing process is complete, the drawing gets applied on the panel. With other words it's like drawing a picture without showing it to anyone until it's finished.
I tried it. But I'm doin' it wrong. First I draw on a Panel that is only declared (there are no lines in the code that change it's properties). After that I'm trying to apply the new drawing on a visible panel using "gfx = RealPanel.CreateGraphics();" But this only makes a new graphics on the panel... Yep, I'm doin' it wrong. I have no idea how to do it right.
In my case there is a panel that in one moment gets filled with lines and squares. The panel is also belong to another panel. The firs panel cannot de resized along with the whole window, but the second panel can. When panel number 2 is being resized and it's smaller than panel number 1, the flickering begins. 开发者_JAVA技巧The second panel's auto scroll property is set to "true", and when I'm scrolling there is flickering too.
Please don't try telling me to use double buffering. I tried with "unlocking" the double buffering property of the panel. And I tried all the methods explained here: Winforms Double Buffering ... Doesn't make things any different for me. And I want to try this idea.
So, how can I do it right?
Thanks!
You should never have to use CreateGraphics
.
A double-buffered panel should fix this. Don't use it on your "hidden" panel. You should probably get rid of the hidden panel and just use your new panel control. If you still have flickering issues, then you should edit your question and post the code that causes the filckering.
public class PanelEx : Panel
{
public PanelEx()
{
this.DoubleBuffered = true;
}
}
If you still want to do your own "hidden" double-buffering, then you do it straight on your own bitmap:
Bitmap bm = new Bitmap(width, height);
Then do your drawing like this:
using (Graphics g = Graphics.FromImage(bm))
{
//Draw stuff
}
In your Panels paint event, all you do is:
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(bm, 0, 0);
}
Whenver you need to update the screen, just invalidate the panel:
panel1.Invalidate();
which will tell your program to call the paint event.
精彩评论