Export (0) Print
Expand All

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.

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;

}

This example requires:

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

Community Additions

ADD
Show:
© 2014 Microsoft