From: Tony Johansson on
Hello!

This code works fine when added to event handler for the forms paint event.
If I instead add the three rows to the Forms c-tor
nothing is being drawn in the form why?
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bm = new Bitmap("background3.bmp");
Graphics g = this.CreateGraphics();
g.DrawImage(bm, 1, 1, this.Width, this.Height);
}

I can for example add these two rows to the forms c-tor and it works fine.
Image i = Image.FromFile("background3.bmp");
this.BackgroundImage = i;

//Tony


From: Peter Duniho on
Tony Johansson wrote:
> Hello!
>
> This code works fine when added to event handler for the forms paint event.
> If I instead add the three rows to the Forms c-tor
> nothing is being drawn in the form why?

Something is being drawn, but because (as you already know) drawing to
Forms controls is event-driven, where the control must be redrawn by the
Paint event every time Windows asks it to, when the form is redrawn
after everything else is initialized, whatever you drew in the
constructor is just over-drawn.

> private void Form1_Paint(object sender, PaintEventArgs e)
> {
> Bitmap bm = new Bitmap("background3.bmp");
> Graphics g = this.CreateGraphics();
> g.DrawImage(bm, 1, 1, this.Width, this.Height);
> }
>
> I can for example add these two rows to the forms c-tor and it works fine.
> Image i = Image.FromFile("background3.bmp");
> this.BackgroundImage = i;

Of course. That's because instead of trying to draw the image in the
constructor, you are setting the background image and allowing the
control to redraw the relevant part of the form every time Windows asks
it to. That's the correct way to do it (well, it's the correct way to
implement a background image, anyway).

Pete
From: Jeff Johnson on
"Tony Johansson" <johansson.andersson(a)telia.com> wrote in message
news:%23hIrdbHDLHA.4308(a)TK2MSFTNGP04.phx.gbl...

> This code works fine when added to event handler for the forms paint
> event.
> If I instead add the three rows to the Forms c-tor
> nothing is being drawn in the form why?

For reasons similar to what is discussed in this article:
http://www.bobpowell.net/picturebox.htm