Export (0) Print
Expand All

How to: Set a Background Image on a Form 

You can override the form's OnPaint method to draw an image as the background for your form.

To draw a background image on a form

  1. Override the form's OnPaint method

  2. Get the image from an external file on the device or as an embedded resource in the assembly.

  3. Use the Graphics object from the Graphics property of the PaintEventArgs to draw the image. Use the dimensions specified by the form's ClientRectangle property

Example

This example uses an image file compiled as an embedded resource as the background image for a form.

protected override void OnPaint(PaintEventArgs e)
{        
    // Get image compiled as an embedded resource.
    Assembly asm = Assembly.GetExecutingAssembly();
    Bitmap backgroundImage = new Bitmap(asm.GetManifestResourceStream("mypicture.jpg"));

    e.Graphics.DrawImage(backgroundImage, this.ClientRectangle,
        new Rectangle(0,0, backgroundImage.Width, backgroundImage.Height),
        GraphicsUnit.Pixel);
}

Compiling the Code

This example requires references to the following namespaces:

See Also

Community Additions

ADD
Show:
© 2014 Microsoft