Graphics and GDI

Source: Internet
Author: User

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:

 
 
  1. private void Form1_Paint(object sender,System.Windows.Forms.PaintEventArgs pe)  
  2. {  
  3.      Graphics g=pe.Graphics;  

Call the creategraphics method of a form or control to create a graphics object, for example, the following code:

 
 
  1. Graphics g=pe.Graphics;  
  2. g=this.CreateGraphics(); 

Create a graphics object from an object inherited from an image, for example, the following code:

 
 
  1. Bitmap mm=new Bitmap(@"D:\123.bmp");  
  2. 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.

 
 
  1. Color Mm = color. formargb (0,255 );
  2. // Opaque blue
  3. Color nn = color. formargb (128,255 );
  4. // 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.

 
 
  1. 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.

 
 
  1. Color Mm = new solidbrush (color. formargb (255, 0, 0,255 ));
  2. // Opaque blue solid paint brush
  3. Color nn = new solidbrush (color. formargb (128,255 ));
  4. // 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:

 
 
  1. LinearGradientBrush mm = new LinearGradientBrush(new Point(0,10)),  
  2. new Point(0,10),  
  3. new Point(200,10),  
  4. Color.FormArgb(255,255,0,0),  
  5. 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.

 
 
  1. LinearGradientBrush mm = new LinearGradientBrush(new Point(0,10)),  
  2. new Point(10,0),  
  3. new Point(10,200),  
  4. Color.FormArgb(255,255,0,0),  
  5. Color.FormArgb(255,0,0,255)); 

The following code creates a diagonal gradient paint brush.

 
 
  1. LinearGradientBrush mm = new LinearGradientBrush(new Point(0,10)),  
  2. new Point(0,0),  
  3. new Point(200,200),  
  4. Color.FormArgb(255,255,0,0),  
  5. 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:

 
 
  1. Pen mm = new Pen (Color.Black,8);  
  2. Pen nn = new Pen (Color.Red);  
  3.  nn.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;  
  4.  mm.Color = Color.Pink;  
  5.  mm.Width = 4;  
  6.  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.

  1. Pen Mm = new pen (color. formargb (0,255,), 15); // opaque pen
     
  2. Pen nn = new pen (color. formargb (0,255, 0,), 15 );

    // Transparent pen

Reprinted from: http://book.51cto.com/art/200907/139870.htm

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.