How to: Create a Bitmap at Run Time

This example creates and draws in a Bitmap object and displays it in an existing Windows Forms PictureBox control.

Example

PictureBox pictureBox1 = new PictureBox();
public void CreateBitmapAtRuntime()
{
    pictureBox1.Size = new Size(210, 110);
    this.Controls.Add(pictureBox1);

    Bitmap flag = new Bitmap(200, 100);
    Graphics flagGraphics = Graphics.FromImage(flag);
    int red = 0;
    int white = 11;
    while (white <= 100) {
        flagGraphics.FillRectangle(Brushes.Red, 0, red, 200,10);
        flagGraphics.FillRectangle(Brushes.White, 0, white, 200, 10);
        red += 20;
        white += 20;
    }
    pictureBox1.Image = flag;

}

Compiling the Code

This example requires:

  • A Windows Form that imports the System, System.Drawing and System.Windows.Forms assemblies.

See Also

Reference

Bitmap

Other Resources

Images, Bitmaps, and Metafiles