Abusing
PictureBox
is a usual mistake. Of course you can draw on an instance of a
System.Windows.Forms.PictureBox
, but there are no situations when it can be useful. It can be useful to draw in an instance of
System.Drawing.Image
or an instance of any control class, usually derived from
System.Windows.Forms.Control
.
As to the
PictureBox
, it is designed to make it very simple to present pictures, one whole static picture at a time. An attempt to do anything dynamic, interactive or animated is just waste of extra resources and development time. It does not present any additional feature, only the hassles. Even your simple example would be an abuse. You can do it, but you never need it.
Not to worry, I'll explain what to do instead.
To draw on the instance of
Image
, obtain an instance of
System.Drawing.Graphics
using the static factory method
System.Drawing.Graphics.FromImage(Image)
and draw using
Graphics
, please see
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx[
^].
In contrast, you should never create an instance of
Graphics
if you need to render graphics on screen, using any
Control
. Instead, you should obtain one from the event arguments parameter passed to the overridden method
OnPaint
or your event handler of the event
Paint
, please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[
^].
Please find further detail on rendering on-screen graphics in my past solutions:
How do I clear a panel from old drawing[
^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[
^],
Drawing Lines between mdi child forms[
^],
capture the drawing on a panel[
^].
—SA