C uses GDI to draw common graphics and text

  • 2021-09-20 21:19:44
  • OfStack

Needless to say, let's get to know this GDI + first and see what it looks like.

GDI +: Graphics Device Interface Plus is also a graphics device interface, which provides a variety of rich graphics and image processing functions; In C #. NET, GDI + is used to process 2D graphics and images, and DirectX is used to process 3D graphics and images. The main namespace used in graphics and image processing is System. Drawing: Provides access to basic graphics functions of GDI +, mainly including Graphics, Bitmap, classes inherited from Brush, Font, Icon, Image, Pen, Color, etc.

After understanding what GDI + is, let's show you what it can do with a few examples.

Step 1 Draw a straight line

private void btnDrawLine_Click(object sender, EventArgs e)
{
    // Create 1 Drawing surface
    Graphics g = this.CreateGraphics();
    // Create 1 A pen
    Pen pen = new Pen(Brushes.Red);
    // Two coordinates of a straight line
    Point pointStart = new Point(10, 10);
    Point pointEnd = new Point(50, 50);
    // Start drawing
    g.DrawLine(pen, pointStart, pointEnd);
}

Step 2 Draw a rectangle

private void btnDrawRectangle_Click(object sender, EventArgs e)
{
    // Create 1 Drawing surface
    Graphics g = this.CreateGraphics();
    // Create 1 A pen
    Pen pen = new Pen(Brushes.DeepSkyBlue);
    // Determine the starting point, length and height of the rectangle
    g.DrawRectangle(pen, 50, 50, 60, 30);
}

3. Draw a string

private void btnDrawString_Click(object sender, EventArgs e)
{
    Graphics g = this.CreateGraphics();
    Font font = new Font(" Huawei Song Style ", 12);
    //Point1 Sample, except that the value is a floating-point type
    PointF point = new PointF(50, 50);
    g.DrawString(" I am Kimisme", font, Brushes.Coral, point);
}

Step 4 Draw a fan

private void btnDrawSector_Click(object sender, EventArgs e)
{
    Graphics g = this.CreateGraphics();
    g.DrawPie(new Pen(Brushes.Green),
        new Rectangle(new Point(100, 70),
            new Size(50, 50)), 60, 90);
}


Related articles: