PictureBox PaintEvent with other method

You need to decide what you want to do:

  • Draw into the Image or
  • draw onto the Control?

Your code is a mix of both, which is why it doesn’t work.

Here is how to draw onto the Control:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawEllipse(Pens.Red, new Rectangle(3, 4, 44, 44));
    ..
}

Here is how to draw into the Image of the PictureBox:

void drawIntoImage()
{
    using (Graphics G = Graphics.FromImage(pictureBox1.Image))
    {
        G.DrawEllipse(Pens.Orange, new Rectangle(13, 14, 44, 44));
        ..
    }
    // when done with all drawing you can enforce the display update by calling:
    pictureBox1.Refresh();
}

Both ways to draw are persistent. The latter changes to pixels of the Image, the former doesn’t.

So if the pixels are drawn into the Image and you zoom, stretch or shift the Image the pixel will go with it. Pixels drawn onto the Top of the PictureBox control won’t do that!

Of course for both ways to draw, you can alter all the usual parts like the drawing command, maybe add a FillEllipse before the DrawEllipse, the Pens and Brushes with their Brush type and Colors and the dimensions.

Leave a Comment