Analysis of GDI programming under the .NET framework
Author: Wang Kaiming This article taken from: SEOUL
2002
Year 12
Yue 17
day
At present, Microsoft's .NET framework is further developed, and 1.1 versions will be released, accompanied by new concepts such as namespace, Windows Form, GDI , and CLRs, new mechanisms. This article will introduce some of the basic knowledge of GDI programming under the .NET framework.
GDI is evolved from GDI, but in the previous version of Visual Studio, GDI is quite complicated, and the workload is huge. Now in GDI , Microsoft has helped us solve many problems, so it will become very easy to use GDI programming.
GDI is included in the System.drawing.dll collection, all GDI classes are included in the namespaces such as System.drawing, System.Text, System.Printing, System.Drawing2D, and System.Design.
This article first introduces you a graphics class. Then, you will introduce some of the most common class and structures, including brush (PEN), Brush, Font, Color (Color) or otherwise. The article also gave some examples of the use of the native language C # implementation of the .NET framework.
Graphics Class
We use the objects of the graphics to represent the GDI graphics surface. In order to use GDI , we must first create a graphic object. Typically, we can get a reference to a graphic object from a Paint event or to achieve the object by overpaint overpaint. The specific method is as follows:
Private Void Form1_Paint (Object Sender, Painteventargs PE)
{
Graphics g = pe.graphics;
}
Or:
Protected Override Void OnPaint (Painteventargs PE)
{
Graphics g = pe.graphics;
}
After establishing a graphic object, we can call the following methods to complete the basic drawing feature.
Drawarc
(Have been overloaded) draw a arc
DrawCloseDcurve
(Overloaded) drawing a closed fold line determined by some points
Drawcurve
(The overloaded) draws a fold line determined by some points
Drawellipse
(Overloaded) draw an ellipse
DrawImage
(Overloaded) draw a pair image
Drawline
(Have been overloaded) painting a straight line
Drawpath
Painted a path (including straight lines and curves)
Drawpie
(Overloaded) painting a pair of pieces
DrawPolygon
(Overloaded) painted a polygonal contour
DrawRectangle
(Has been overloaded) draw a rectangular contour
Drawstring
(Overloaded) painted a string
FILLLLIPSE
(Overloaded) filled an ellipse area
FillPath
Plug a path
Fillpie
(Overloaded) Plucking a piece of pies FillPolygon
(Overloaded) populate a polygon
FillRectangle
Fill a rectangle with painting brush
FillRectangles
Fill a series of rectangles with painting brush
FillRegion
Fill a zone
Graphic object
After establishing a graphic object, we can use it to draw lines, fill graphics, and draw text, and so on. Here are some main graphics objects:
Brush
Used to populate specific surfaces
Pen
Used to paint straight lines, polygons, rectangles, arcs, and pie districts, etc.
Font
Font used to set text
Color
Used to set the color of a specific object (in GDI , the color can be alpha mixed)
Brush Class
The painting class is an abstract base class, we can't instantiate it directly. We must instantiate its sub-objects, which include: Solidbrush, Texturebrush, RectangleGradientBrush, and LinearGradientBrush.
For example, as follows:
LINEARGRADIENTBRUSH LBRUSH = New Lineargradientbrush (Rect, Color.red,
Color.Yellow,
Lineargradientmode.BackWardDiagonal);
Brush Brsh = New Solidbrush (Color.Red), 40, 40, 140, 140);
The Solidbrush class defines a brush consisting of monochrome. This painting can be used to fill in a rectangular, elliptical, tart, polygon, and a pattern area such a path.
The Texturebrush class defines a brush that can be filled with a certain area.
The LinearGradiantBrush class can define a painted brush between two colors, or a multi-color changing brush.
Pain Class (Pen Class)
Brush is used to draw a straight line and curve with specific widths and styles. We must first initialize a brush object with the constructor of the brush class, and you can use color and paintings during instantiation.
Initialize new brush objects with specific colors:
Public Pen (Color);
Initialize the new brush object with a specific painting brush:
Public Pen (brush);
Initialize the new brush object with a specific painting and width:
Public Pen (brush, float);
Initialize new brush objects with specific colors and width:
Public Pen (Color, Float);
For example, as follows:
Pen pn = new pen (color.blue);
Or:
PEN PN = New Pen (Color.Blue, 100);
The following is some of the most commonly used properties of the brush class:
Alignment
Get or set the boundary of the object with the brush painting
Brush
Get or set brush to determine the brush characteristics
Color
Get or set the color of the brush
Width
Get or set the width of the brush
Font class (Font Class)
The font class determines the font format of a particular text, such as the font type, size, and style. We build a font with the constructor of the font class. Initialize new font objects with specific properties:
Public Font (String, Float);
Initialize new font objects with specific existing fonts and font styles:
Public Font (Font, FontStyle);
Here are some font styles:
Bold
Bold
Italic
Bevel
Regular
Normal font
Strikeout
Remove line
Underline
Underline
For example, as follows:
Graphics G; Font Font = New Font ("Times New Roman", 26);
Color structure
A color structure represents a color in a Argb format. The following is its argb attribute: A: Get color Alpha ingredient value B: Get color blue ingredient value G: Get color's green component value R: Get color's red component value below how the color structure is used:
Pen pn = new pen (color.blue);
To now, I believe that everyone has a general understanding of GDI under the .NET framework. In order to make everyone more intuitive understandings about GDI programming knowledge, I specially prepared some basic but useful examples to everyone. Through the study of these examples, I believe that everyone will have a more profound understanding of GDI programming under the .NET framework. At the same time, the following example is implemented with C # language, if you are a VB.NET enthusiast, you may wish to achieve the same functionality by appropriately modifying the code. Draw a rectangle:
Protected Override Void OnPaint (Painteventargs PE)
{
Graphics g = pe.graphics;
/ / Set the position and size of the rectangular area
Rectangle Rect = New Rectangle (0, 0, 200, 200);
/ / Make the color of the filled rectangle from red to yellow gradient
LINEARGRADIENTBRUSH LBRUSH = New Lineargradientbrush (Rect, Color.red, Color.Yellow,
Lineargradientmode.BackWardDiagonal);
g.fillRectangle (LBRUSH, RECT);
}
The illustration is as follows:
Draw an ellipse:
Protected Override Void OnPaint (Painteventargs PE)
{
Graphics g = pe.graphics;
/ / Create a 100 pixel wide, blue brush
PEN PN = New Pen (Color. Forestgreen, 100);
Rectangle Rect = New Rectangle (50, 50, 180, 100);
g.drawellipse (PN, RECT);
}
The illustration is as follows:
Draw a piece of text:
Protected Override Void OnPaint (Painteventargs PE)
{
Graphics g = pe.graphics;
// Text content is "Welcome to the graphics world!"
g.drawstring ("Welcome to the Graphics World!", this.Font, New Solidbrush (Color.Red), 10, 10;
}
The illustration is as follows:
Painting a straight line:
Protected Override Void OnPaint (Painteventargs PE)
{
Graphics g = pe.graphics;
Pen PN = New Pen (Color.Blue, 10);
// set two points in advance
Point Pt1 = New Point (30, 30);
Point PT2 = New Point (110, 100);
g.drawline (PN, PT1, PT2);
}
The illustration is as follows:
Painting a paragraph:
Protected Override void onpaint (Painteventargs PE) {
Graphics g = pe.graphics;
/ / Create a 20 pixel wide, pink and translucent brush
Pen Penexample = New Pen (Color.Fromargb (150, Color.Purple), 20);
// Make the brush to draw a broken line
Penexample.dashstyle = dashstyle.dash;
// Set the brush to the end
Penexample.startcap = linecap.Round;
Penexample.endcap = linecap.Round;
// Now use a brush painting curve
g.drawcurve (Penexample, New Point [] {
New Point (100, 70),
New Point (350, 120),
New Point (250, 170),
New Point (70, 70),
New Point (20, 170),
});
}
The illustration is as follows:
Draw a polygonal graphic with a gradual change in color:
Protected Override Void onPaint (Painteventargs E) // This is an overpaint function
{
E.Graphics.TextRenderingHint = system.drawing.text.TextRenderingHint.ntialias;
E.Graphics.FillRectangle (New Solidbrush (Color.Fromargb (180, Color.White),
ClientRectangle;
// Establish a graphic path
GraphicsPath path = new graphicspath (new point [] {
New Point (40, 40),
New Point (275, 100),
New Point (105, 125),
New Point (190, 200),
New Point (50, 250),
New Point (20, 80),
}, new byte [] {
(Byte) PathPointType.start,
(Byte) PathPointType.bezier,
(Byte) PathPointType.bezier,
(Byte) PathPointType.bezier,
(Byte) PathPointType.Line,
(Byte) PathPointType.Line,
});
// Create a pathGradientBrush object
PathGradientbrush PGB = New pathgradientbrush (path);
Pgb.surroundcolors = new color [] {
Color.green,
Color.Yellow,
Color.Red,
Color.Blue,
Color.range,
Color.White,
}
// Finally filled
E.Graphics.FillPath (PGB, PATH);
}
The illustration is as follows:
(Note: The above examples need to be implemented in a Windows2000 Server version or Windows XP Professional and VS.NET environments)