Setting background image on run time.

  • Thread starter Thread starter Mr. X.
  • Start date Start date
M

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 =
(Image)(Image.FromHbitmap(bmp.GetHbitmap()));


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

Why ?
 
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)
{
try
{
if (Lock)
{
sendMessage((long)c.Handle, WM_SETREDRAW, 0, 0);
}
else
{
sendMessage((long)c.Handle, WM_SETREDRAW, 1, 0);
c.Refresh();
}
}
catch (Exception err)
{
}
}

I meant I cannot do in code, first time (on the OnPaint event)
LockUpdate(true);
....
do some painting
....
LockUpdate(false);

Thanks :)
 
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 :)
 
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.

Pete
 
Back
Top