c# GDI + simple drawing

Author: stg609

The copyright of this article belongs to the author and the blog park. Reprint is welcome, but this statement must be retained without the consent of the author, and the original connection must be given in an obvious position on the article page, otherwise the right to investigate legal responsibility is reserved.

In the previous film, we have introduced how to use GDI + to draw simple images. This film continues to introduce you to other drawing knowledge

1. First, let's look at the Pen we used in the previous film

Pen attributes mainly include: color, dashcap, dashstyle, endcap, startcap, width, etc
We can use Pen to draw dotted lines, straight lines with arrows, etc

Pen  p = new  Pen(Color.Blue, 5);//Set the pen thickness to and the color to blue
Graphics  g = this.CreateGraphics();

//Draw a dotted line
p.DashStyle = DashStyle.Dot;//Defines the style of the dashed line as a point
g.DrawLine(p, 10, 10, 200, 10);

//Custom dashed line
p.DashPattern = new  float[] { 2, 1 };//Sets an array of dashes and whitespace
g.DrawLine(p, 10, 20, 200, 20);

//Drawing arrows is only useful for unclosed curves
p.DashStyle = DashStyle.Solid;//Restore solid line
p.EndCap = LineCap.ArrowAnchor;//Defines the style of the line tail as an arrow
g.DrawLine(p, 10, 30, 200, 30);


Operation results of the above code:

2. Next, let's look at the use of Brush

Function: we can fill various graphic shapes with brushes, such as rectangle, ellipse, fan, polygon and closed path. There are several different types of brushes:
• SolidBrush: the simplest form of brush, painted in solid color

• HatchBrush: similar to SolidBrush, but it can be used to select the pattern to be used when drawing from a large number of preset patterns instead of solid color

• TextureBrush: use textures (such as images) to paint

• LinearGradientBrush: paint with two colors mixed along the gradient

• PathGradientBrush: Based on the unique path defined by the programmer, draw with complex mixed color gradients

Here we just briefly introduce some of them:

Graphics g = this.CreateGraphics();
Rectangle rect = new Rectangle(10, 10, 50, 50);//Define a rectangle, with parameters as the abscissa and ordinate of the starting point and its length and width

//Monochrome fill
SolidBrush b1 = new SolidBrush(Color.Blue);//Define monochrome brushes          
g.FillRectangle(b1, rect);//Fill this rectangle

//character string
g.DrawString("character string", new Font("Song typeface", 10), b1, new PointF(90, 10));

//Fill with picture
TextureBrush b2 = new TextureBrush(Image.FromFile(@"e:\picture\1.jpg"));
rect.Location = new Point(10, 70);//Change the start coordinates of this rectangle
rect.Width = 200;//Change the width of this rectangle to
rect.Height = 200;//Change the height of this rectangle
g.FillRectangle(b2, rect);

//Fill with gradient
rect.Location = new Point(10, 290);
LinearGradientBrush b3 = new  LinearGradientBrush(rect, Color.Yellow , Color.Black , LinearGradientMode.Horizontal);
g.FillRectangle(b3, rect);

Operation effect diagram:

3. Coordinate axis transformation

The coordinate axis in winform is different from the plane rectangular coordinate axis we usually contact. The direction of the coordinate axis in winform is completely opposite: the upper left corner of the form is the origin (0,0), the horizontal left increases X, and the vertical down increases Y

Next, we will draw patterns at different angles by rotating the direction of the coordinate axis, or balance the position of the coordinate axis by changing the position of the coordinate origin

Graphics g = this.CreateGraphics();

//Monochrome fill
//SolidBrush b1 = new SolidBrush(Color.Blue);// Define monochrome brushes          
Pen p = new Pen(Color.Blue,1);

//Transition axis angle
for (int i = 0; i < 90; i++)
    g.RotateTransform(i);//Draw a line for each degree of rotation
    g.DrawLine(p, 0, 0, 100, 0);
    g.ResetTransform();//Restore axis coordinates

//Translation axis
g.TranslateTransform(100, 100);
g.DrawLine(p, 0, 0, 100, 0);

//First pan to the specified coordinates, and then rotate by degrees
for (int i = 0; i < 8; i++)
g.DrawLine(p, 0, 0, 100, 0);


Operation effect diagram:

4. Finally, let's see what else we can draw on the Graphics drawing board

In fact, what we use above is to draw some simple graphics, such as lines, rectangles, sectors, circles, etc. we can also use it to draw pictures, which can use its DrawImage method. I won't explain it in detail here. If you are interested, you can go to MSDN to understand it. This method will be used in the screenshot we will talk about later

Tags: C# Back-end

Posted by ftrudeau on Thu, 21 Oct 2021 05:49:53 +0530