Share via


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 Overrides Sub OnPaint(e As PaintEventArgs)

    ' Get image compiled as an embedded resource.
    Dim asm As Assembly = Assembly.GetExecutingAssembly()
    Dim backGroundImage As New Bitmap(asm.GetManifestResourceStream("mypicture.bmp"))
    
    e.Graphics.DrawImage(backgroundImage, Me.ClientRectangle, _
        New Rectangle(0, 0, backgroundImage.Width, backgroundImage.Height), _
        GraphicsUnit.Pixel)

End Sub   
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

Concepts

Custom Control Development

Other Resources

Windows Forms Controls in the .NET Compact Framework