In addition to Solution 1:
This is right pattern of using disposable objects:
using (Bitmap someBitmap = new Bitmap()) {
Graphics graphics =
graphics.DrawImage(someBitmap, );
}
This code is strictly equivalent to using try-finally block with calling
Dispose
in the
finally
block:
http://msdn.microsoft.com/en-us/library/yh598w02.aspx[
^].
Besides, don't create an instance in of the
Graphics
in your case (you need to create it in very rare cases, not in yours). Instead, use the instance passed as a event argument parameter to the overridden method
Control.OnPaint
or your event handler of the event
Control.Paint
. Please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[
^],
capture the drawing on a panel[
^],
Drawing Lines between mdi child forms[
^].
And, by the way, my other answers explain why you hardly should use the control
PictureBox
:
Append a picture within picturebox[
^],
draw a rectangle in C#[
^],
How do I clear a panel from old drawing[
^].
Good luck.
—SA