First of all, don't even play with the idea of using
PictureBox
.
This control is poorly redundant and should be used only to simplify the simplest cases, such as showing some static image. In most more complex cases, it only presents hassles and use resources giving nothing in return. You should use your own rendering of graphics, which is really simple. Please see my past answers:
How do I clear a panel from old drawing[
^],
draw a rectangle in C#[
^],
Append a picture within picturebox[
^].
Now, to draw on a bitmap, you need to obtain the instance of
System.Drawing.Graphics
from this bitmap. This is how:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx[
^].
Now, you can use this image in a usual way, to draw on a bitmap.
Again, check your design thoroughly. If the only purpose of drawing on a bitmap was drawing on screen, you don't even need a bitmap. The bitmap will be used internally to avoid
flicker, if you simply use double buffering. On further rendering detail, please see my past answers:
Drawing Lines between mdi child forms[
^],
capture the drawing on a panel[
^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[
^],
How to speed up my vb.net application?[
^].
If you still need to draw on a bitmap (for example, if you need to save graphics as bitmap), do as I explained before. But if you need to draw on both (bitmap, screen, maybe printer as well), pay attention that they all use
System.Drawing.Graphis
. Write some universal method using
Graphics
as a parameter, with other parameters, such as size. This way, you can abstract out your rendering from a particular media.
—SA