background image in a form

  • Thread starter Thread starter Eka Gautama
  • Start date Start date
E

Eka Gautama

Hi there,

I have couple questions:
- how can i put a background image in a form
- how can i set a trasparent bg color a label or button..

Thanks for help..
 
You can overload the paint event:

instantiate a Bitmap called backgroundImage and set it in the constructor
then use this code:

protected override void OnPaintBackground(PaintEventArgs e)

{

// use image for background

e.Graphics.DrawImage(backgroundImage, 0, 0);

}
 
To achieve the first point you can override the OnPaint method of the Form
and draw an image to the form. e.g.
[C#]
protected override void OnPaint(PaintEventArgs e)

{

//paint the splash screen bitmap

Graphics g = e.Graphics;

g.DrawImage(new
Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResource
Stream("background.bmp")),0, 0);

base.OnPaint (e);

}



[VB]

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

Graphics g = e.Graphics

g.DrawImage(New
Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResource
Stream("background.bmp")),0, 0)

MyBase.OnPaint(e)

End Sub



Note here I have loaded a bitmap from an embedded resource too. You could
use the alternate constructor for the Bitmap object which takes a filepath
to a valid image file.

Peter
 
Why is the base.OnPaint(e) method called?


Peter Foot said:
To achieve the first point you can override the OnPaint method of the Form
and draw an image to the form. e.g.
[C#]
protected override void OnPaint(PaintEventArgs e)

{

//paint the splash screen bitmap

Graphics g = e.Graphics;

g.DrawImage(new
Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResource
Stream("background.bmp")),0, 0);

base.OnPaint (e);

}



[VB]

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

Graphics g = e.Graphics

g.DrawImage(New
Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResource
Stream("background.bmp")),0, 0)

MyBase.OnPaint(e)

End Sub



Note here I have loaded a bitmap from an embedded resource too. You could
use the alternate constructor for the Bitmap object which takes a filepath
to a valid image file.

Peter


--
Peter Foot
Windows Embedded MVP

www.inthehand.com | www.opennetcf.org

Eka Gautama said:
Hi there,

I have couple questions:
- how can i put a background image in a form
- how can i set a trasparent bg color a label or button..

Thanks for help..
 
PeterB

just leave it out and you'll see...

just imagine that your base class draws a line over the screen, if you
override the paint event and do not call it, it means that the code of the
parent class is not executed, thus resulting into wanted or unwanted
features..

regards Floris

[cut]
why should i call base.
[/cut]
 
Back
Top