10.3 Basic Drawing
Graphics in. NET include the following two steps.
First, create the Graphics object.
Second, use the Graphics object to draw lines and shapes, render text, or display an action image.
Thus, how to create a Graphics object is the first problem to be solved. This section describes how to draw basic graphics such as lines, rectangles, circles, and so on, and then basic graphics form complex graphics. Graphics commonly used drawing methods are shown in the following table.
10.3.1 Create a drawing object
There are several ways to create a drawing object.
² receives a reference to a drawing object in the Paint event of a form or control as part of a PaintEventArgs. This method is typically used to get a reference to a drawing object when you create the drawing code for the control.
² invokes the CreateGraphics method of a control or form to get a reference to a Graphics object that represents the drawing surface of the control or form. You can use this method if you want to draw on a form or control that already exists.
² Creates a Graphics object from any object inherited from image. This method is commonly used to generate pictures.
In the case of ASP.net Web applications, the first two are not possible, so only a third method can be used.
Suppose you want to create a drawing object from the site root mm.jpg graphic, you can use the following code:
Bitmap bmp = new Bitmap(Server.MapPath("~/mm.jpg"));
Graphics g = Graphics.FromImage(bmp);
bmp.Dispose();
g.Dispose();