When writing graphics programs, GDI needs to be used (Graphics Device Interface). From the perspective of programming, GDI includes two parts: one is a GDI object and the other is a GDI function. GDI objects define tools and environment variables used by GDI functions, while GDI functions use GDI objects to draw various graphics. In C#, the GDI+ (Graphice Device Interface Plus Graphics Device Interface) version is used when writing graphics programs. GDI+ is a further extension of GDI, which makes us more convenient to program.
GDI+ Overview
GDI+ is a new graphics device interface provided by Microsoft in the operating system after Windows 2000. It is displayed through a set of classes deployed as managed code. This set of classes is called the "hosted class interface" of GDI+. GDI+ mainly provides the following three types of services:
(1) Two-dimensional vector graphics: GDI+ provides a class (or structure) that stores the information of the graphic primitives itself, a class that stores the information of the drawing method of the graphic primitives, and a class that actually draws.
(2) Image processing: Most pictures are difficult to delineate as a collection of straight lines and curves, and cannot be processed using two-dimensional vector graphics. Therefore, GDI+ provides us with Bitmap, Image and other classes, which can be used to display, operate and save BMP, JPG, GIF and other image formats.
(3) Text display: GDI+ supports the use of various fonts, font sizes and styles to display text.
If we want to perform graphical programming, we must first explain the Graphics class, and we must also master the types of Pen, Brush and Rectangle. GDI+'s superiority over GDI is mainly reflected in two aspects: (first) GDI+ expands the functions of GDI by providing new functions (such as gradient brush and alpha mix); (second) revised the programming model to make graphics programming easier and more flexible.
Graphics
The Graphics class encapsulates a GDI+ drawing surface and provides a method to draw objects to display devices, which are associated with specific device contexts. The drawing methods are included in the Graphics class. When drawing any object (for example: Circle, Rectangle), we first need to create a Graphics class instance. This instance is equivalent to creating a canvas. Only with the canvas can we use various drawing methods to draw.
The design process of drawing programs is generally divided into two steps:
- (1) Create Graphics object;
- (2) Use Graphics objects to draw, display text or process images.
Usually we use the following three methods to create a Graphics object.
Method 1. Use PainEventArgs in the Paint event of a control or form to receive a reference to a graphic object in the Paint event of a form or control. As part of PaintEventArgs (PaintEventArgs specifies the Graphics used to draw the control). When creating drawing code for the control, this method is usually used to obtain a reference to the graphic object.
For example:
//Response method of Paint event in formprivate void form1_Paint(object sender, PaintEventArgs e) { Graphics g = ; }
You can also directly overload the OnPaint method of the control or form, the specific code is as follows:
protected override void OnPaint(PaintEventArgs e) { Graphics g = ; }
The Paint event occurs when the control is redrawn.
Method 2: Call the CreateGraphics method of a control or form
Call the CreateGraphics method of a control or form to obtain a reference to a Graphics object that represents the drawing surface of the control or form. This method is usually used if you want to draw on an existing form or control.
For example:
Graphics g = ();
Method 3: Calling the FromImage static method of Graphics class
Creates a Graphics object from any object inherited from Image. This method is usually used when you need to change an existing image.
For example:
//The image named "" is located in the current pathImage img = ("");//Create Image objectGraphics g = (img);//createGraphicsObject
1. Method members of Graphics class
After a Graphics object reference, you can use the members of the object to draw various graphics. Table 1 lists the common method members of the Graphics class.
name | illustrate |
DrawArc |
Draw an arc. |
DrawBezier | Draw three-dimensional Belsey curves. |
DrawBeziers | Draw continuous three-dimensional Belsey curves. |
DrawClosedCurve | Draw a closed curve. |
DrawCurve | Draw curves. |
DrawEllipse | Draw an oval. |
DrawImage | Draw images. |
DrawLine | Draw lines. |
DrawPath | Draw lines and curves through the path. |
DrawPie | Draw a pie shape. |
DrawPolygon | Draw polygons. |
DrawRectangle | Draw a rectangle. |
DrawString | Draw text. |
FillEllipse | Fill the ellipse. |
FillPath | Fill the path. |
FillPie | Fill the pie chart. |
FillPolygon | Fill polygons. |
FillRectangle | Fill the rectangle. |
FillRectangles | Fill the rectangle group. |
FillRegion | Fill the area. |
In .NET, all drawing functions of GDI+ are included in the namespaces such as System, .Darwing2D and etc. Therefore, before starting to use the GDI+ class, you need to reference the corresponding namespace first.
2. Reference namespace
Using the using command in a C# application has used the given namespace or class. Here is an example of a C# application referencing a namespace:
using System; using ; using ; using ; using ; using .Drawing2D; using ;
Commonly used drawing objects
After creating the Graphics object, you can use it to start drawing, you can draw lines, fill graphics, and display
Text, etc., the main objects used are: Color: used to fill with patterns, colors or bitmaps.
Pen: Used to draw lines and polygons, including rectangles, circles and pie shapes.
Font: Used to set the font format for text.
Brush: Used to describe the color.
Rectangle: Rectangle structure is usually used to draw rectangles on a form.
Point: Describes an ordered pair of x and y coordinate values.
1. Pen class
Pen is used to draw straight lines with specified width and style. Draw several dashed lines using the DashStyle property, and you can use a variety of fill styles (including solid colors and textures) to fill the line drawn by the Pen. The fill mode depends on the brush or the texture used as the fill object.
When using a brush, you need to instantiate a brush object first. There are several main methods.
The method of instantiating a brush with the specified color is as follows:
public Pen(Color);
The method of instantiating a brush with the specified brush is as follows:
public Pen(Brush);
The method of instantiating a brush with the specified brush and width is as follows:
public Pen(Brush, float);
The method of instantiating a brush with the specified color and width is as follows:
public Pen(Color, float);
The statement format for instantiating the brush is as follows:
Pen pn=new Pen();
or
Pen pn=new Pen(,100);
There are several commonly used properties of Pen, as shown in the following table:
name | illustrate |
Alignment | Get or set the alignment of the brush. |
Brush | Get or set the properties of the brush. |
Color | Get or set the color of the brush. |
Width | Get or set the width of the brush. |
kind
In nature, colors are mostly composed of transparency (A) and three primary colors (R, G, B). In GDI+, the definition of color is encapsulated through the Color structure. In addition to providing (A, R, G, B), many system-defined colors are also provided, such as Pink (pink color), and many static members are provided for operating colors. The basic properties of the Color structure are shown in the following table:
name | illustrate |
A | Get the alpha component value of this Color structure, and take the value (0~255). |
R | Get the red component value of this Color structure and take the value (0~255). |
G | Get the green component value of this Color structure, and take the value (0~255). |
B | Get the blue component value of this Color structure, and take the value (0~255). |
Name | Gets the name of this Color structure, which returns the name of the user-defined color or the name of the known color (if the color was created from a name), and for a custom color, the RGB value will be returned. |
The basic (static) methods of the Color class are shown in the following table:
name | illustrate |
FromArgb | Create a Color object from four 8-bit ARGB components (alpha, red, green, and blue) values. |
FromKnowColor | Creates a Color object from the specified predefined color. |
FromName | Create a Color object from the specified name of the predefined color. |
Objects of the Color class can be constructed through existing colors or created through RGB, for example:
Color clr1 = (122,25,255); Color clr2 = ();//KnownColor is an enum typeColor clr3 = ("SlateBlue");
In image processing, it is generally necessary to obtain or set the color value of a pixel. The specific steps for obtaining a pixel color value of an image are as follows:
(1) Define Bitmap
Bitmap myBitmap = new Bitmap("c:\\MyImages\\");
(2) Define a color variable to store the pixel value obtained at the specified position into the color variable
Color c = new Color(); c = (10,10);//Get thisBitmapThe color of the specified pixel。
(3) Decompose the color value into monochrome component value
int r,g,b; r= ; g=; b=;
kind
The Font class defines specific text formats, including font, font size, and glyph attributes. The commonly used constructor of the Font class is public Font(string font name, float font size, FontStyle glyph), where the font size and font are optional and public Font(string font name, float font size), where "font name" is FontFamily's string representation form. Here is an example code for defining a Font object:
FontFamily fontFamily = new FontFamily("Arial"); Font font = new Font(fontFamily,16,,);
Common font properties are shown in the following table:
name | illustrate |
Bold | Whether it is bold. |
FontFamily | Font members. |
Height | High font. |
Italic | Whether it is italic. |
Name | Font name. |
Size | Font size. |
SizeInPoints | Gets the font size of this Font object, in points. |
Strikeout | Is there a strikethrough? |
Style | Font type. |
Underline | Is there an underscore? |
Unit | Font size unit. |
kind
The Brush class is an abstract base class, so it cannot be instantiated. We always use its derived class to instantiate a brush object. When we fill the inside of the graphics, we will use brush.
kind
Stores a set of integers, totaling four, representing the position and size of a rectangle. Rectangle structures are usually used to draw rectangles on a form. In addition to using its constructor to construct rectangle objects, you can also use the attribute members of the Rectangle structure, and their attribute members are shown in the following table:
name | illustrate |
Bottom | Bottom coordinates |
Height | Rectangle height |
IsEmpty | Test whether the width and height of the rectangle are 0 |
Left | Coordinates on the left of the rectangle |
Location | The position of the rectangle |
Right | Coordinates to the right of the rectangle |
Size | Rectangular dimensions |
Top | Rectangle top coordinates |
Width | Rectangle width |
X | X coordinate of the upper left corner of the rectangle |
Y | Y coordinate of the upper left corner of the rectangle |
The constructor of the Rectangle structure has the following two:
1. Initialize a new instance of the Rectangle class with the specified location and size.
public Retangle(Point,Size);//SizeStructure stores an ordered pair of integers,Usually the width and height of the rectangle。
2. Initialize a new instance of the Rectangle class using the coordinates of the 4 points of the rectangle.
public Rectangle(int,int,int,int);
kind
Initializes a new instance of the Point class with the specified coordinates. This structure is very similar to the Point structure in C++. It describes an ordered pair of x and y coordinate values. Its constructor is: public Point(int x,int y); where x is the horizontal position of the point; y is the water vertical position of the point.
The following is an example code for constructing a Point object:
Point pt1=new Point(30,30); Point pt2=new Point(110,100);
This is all about this article about C# graphical programming GDI+. I hope it will be helpful to everyone's learning and I hope everyone will support me more.