The graphics class is the core of GDI +. Graphics provides a method to draw objects to display devices. Graphics can be associated with the context of a specific device and is an object used to create a graph. It encapsulates the methods for drawing straight lines, curves, graphs, images, and texts. It is a class for using GDI + to draw straight lines, curves, graphs, images, and texts. It is the basic class of GDI + operations.
The following describes the procedure of drawing with GDI +.
(1) create a graphics object.
(2) draw a line, shape, or text through a graphics object.
There are three methods to create a GDI + object.
You can create a painteventargs class parameter in the form or control's paint event processing method. The painteventargs class parameter contains the reference of the graphic object. For example, the following code:
- private void Form1_Paint(object sender,System.Windows.Forms.PaintEventArgs pe)
- {
- Graphics g=pe.Graphics;
- }
Call the creategraphics method of a form or control to create a graphics object, for example, the following code:
- Graphics g=pe.Graphics;
- g=this.CreateGraphics();
Create a graphics object from an object inherited from an image, for example, the following code:
- Bitmap mm=new Bitmap(@"D:\123.bmp");
- Graphics g= Graphics.FormImage(mm);
After creating the graphics object, you can use the above object to draw lines, shapes, or text.
GDI + defines a series of graphic processing tool classes, which can be used together with graphics objects for drawing processing. The Main drawing tool classes in GDI + are as follows.
1. Color Structure
The color structure indicates the argb color. The values of alpha, red, green, and blue are used to represent the color. The Color Structure defines a large number of named colors, such as color. Red, color. peachpuff, and color. Pink.
The formargb method in the color structure allows you to customize the color by setting four values. For example, the following code creates two colors.
- Color Mm = color. formargb (0,255 );
- // Opaque blue
- Color nn = color. formargb (128,255 );
- // Translucent red
2. Brush class
The brush class defines the objects used to fill the shape of the image. The brush class is an abstract base class and cannot be instantiated. To create a paint brush object, you need a derived class of the brush class.
The solidbrush class defines a monochrome paint brush, which can fill the shape of the image. For example, the following code creates a solid image brush.
- SolidBrush Brush = new SolidBrush(Color.PeachPuff);
You can specify the combination of the shape color and background color by using the Alpha value of the paint brush color. For example, the following code creates two colors.
- Color Mm = new solidbrush (color. formargb (255, 0, 0,255 ));
- // Opaque blue solid paint brush
- Color nn = new solidbrush (color. formargb (128,255 ));
- // Translucent red solid paint brush
The lineargradientbrush class in the system. Drawing. drawing2d namespace defines two-color gradient and custom multi-color gradient Paint Brush. All gradient is defined by the width of the rectangle or the straight line specified by two points. Lineargradientbrush supports linear gradient in the horizontal, vertical, and diagonal directions. The color in the linear gradient can be customized to make the color change unevenly.
See the following code:
- LinearGradientBrush mm = new LinearGradientBrush(new Point(0,10)),
- new Point(0,10),
- new Point(200,10),
- Color.FormArgb(255,255,0,0),
- Color.FormArgb(255,0,0,255));
In the above Code, the constructor mm of lineargradientbrush is used to create a gradient paint brush.
The following code creates a vertical gradient paint brush.
- LinearGradientBrush mm = new LinearGradientBrush(new Point(0,10)),
- new Point(10,0),
- new Point(10,200),
- Color.FormArgb(255,255,0,0),
- Color.FormArgb(255,0,0,255));
The following code creates a diagonal gradient paint brush.
- LinearGradientBrush mm = new LinearGradientBrush(new Point(0,10)),
- new Point(0,0),
- new Point(200,200),
- Color.FormArgb(255,255,0,0),
- Color.FormArgb(255,0,0,255));
3. Pen class
The pen class is used to draw a straight line or curve object. It draws a straight line with a specified width and style through its own properties. You can use the dashstyle attribute to draw a dotted line. The value of the dashstyle attribute is defined by the dashstyle enumeration.
Custom: Set a custom line segment style.
DASH: Set a straight line composed of line segments.
Dashdot: Set a straight line consisting of repeated line points.
Dashdotdot: Set a straight line consisting of the dot pattern of the repeated draw line points.
Dot: Set a straight line composed of points.
Solid: Set a straight line.
See the following code:
- Pen mm = new Pen (Color.Black,8);
- Pen nn = new Pen (Color.Red);
- nn.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
- mm.Color = Color.Pink;
- mm.Width = 4;
- mm.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
In the above Code, the pen class function objects mm and NN are defined respectively, which are used to set the pen color and the actual situation.
Similarly, the combination of the shape color and background color is specified by the Alpha value of the paint brush color. For example, use the following code to create two pen types.
- Pen Mm = new pen (color. formargb (0,255,), 15); // opaque pen
Pen nn = new pen (color. formargb (0,255, 0,), 15 );
// Transparent pen
Reprinted from: http://book.51cto.com/art/200907/139870.htm