Setting background image on run time.


Mr. X.

I have two panels.
One is visible on screen (pnlMain), and one not (BackPanel)

When I create VirtualPanel :
BackPanel = new Panel();
BackPanel.Left = pnlMain.Left;
BackPanel.Top = pnlMain.Top;
BackPanel.Width = pnlMain.Width;
BackPanel.Height = pnlMain.Height;
BackPanel.Margin = pnlMain.Margin;
BackPanel.BackColor = Color.White;

Also I did :
Graphics g = BackPanel.CreateGraphics();
g.DrawEllipse(Pens.Blue,10, 10, 20, 20);

Bitmap bmp = default(Bitmap);

bmp = new Bitmap(pnlMain.Width, pnlMain.Height);
FBackPanel.BackColor = Color.Red;
pnlMain.DrawToBitmap(bmp, new Rectangle(0,
0,pnlMain.Width, pnlMain.Height));
bmp.Save("test.bmp"); // **** even
pnlMain.BackgroundImage =

Not the file "test.bmp" neither pnlMain has the Ellipse (that I did
g.DrawEllipse ...)

Why ?

Mr. X.

I don't want to use onPaint, and draw the ellipse directly on that event
(That's I know).
The reason, is that onPaint take too long time for 100 ellipses,
and I don’t want that when onPaint occurs, I see all the drawing components
refreshing on it.

Also, I couldn’t call LockUpdate :

public static void LockUpdate(Control c, bool Lock)
if (Lock)
sendMessage((long)c.Handle, WM_SETREDRAW, 0, 0);
sendMessage((long)c.Handle, WM_SETREDRAW, 1, 0);
catch (Exception err)

I meant I cannot do in code, first time (on the OnPaint event)
do some painting

Thanks :)

Mr. X.

Major problem, that I can use onPaint, but I see that what I paint get too
much long time,
and I don't want to see user see the process of painting.
(Suppose I am doing a loop that paint 100 ellipses).
I want to create a background with some elements, attach that background to
the panel
(It is fixed background, and should not be changed).

Thanks :)

Mr. X.

Why not count on garbish-collector (GC) ?

Peter Duniho said:
Don't forget to dispose the Graphics instance when you're done with it.
(This was true for your original code too).

You should read about the "using" statement. It will help you ensure
things are disposed.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question