Getting Started with Drawing Graphics with C# GDI

1. Draw a line

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create GDI and draw on the specified panel
            Graphics g = panel1.CreateGraphics();
            //create two points
            Point n1 = new Point(20, 20);
            Point n2 = new Point(100, 100);

            //create brush
            Pen p = new Pen(Brushes.Red,1);
            g.DrawLine(p, n1, n2);
        }
    }
}



2. Draw a rectangle

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create GDI and draw on the specified panel
            Graphics g = panel1.CreateGraphics();
            Pen p = new Pen(Brushes.Red);

            g.DrawRectangle(p, 50, 50, 60, 60);
        }
    }
}


3. Draw a fan

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create GDI and draw on the specified panel
            Graphics g = panel1.CreateGraphics();
            Pen p = new Pen(Brushes.Red);
            Rectangle re = new Rectangle(50, 50, 60, 60);
            g.DrawPie(p,re, 0, 60);  
        }
    }
}


4. Draw a triangle

Triangle class Tiangle

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace WindowsFormsApplication1
{
    public class Triangle
    {
        PointF A, B, C;

        public Triangle(PointF a, PointF b, PointF c)
        {
            A = a;
            B = b;
            C = c;
        }

        /// <summary>
        /// Draw a triangle
        /// </summary>
        /// <param name="g"></param>
        public void Draw(Graphics g)
        {
            Pen pen = new Pen(Color.Red, 1);
            g.DrawLine(pen, A, B);
            g.DrawLine(pen, B, C);
            g.DrawLine(pen, C, A);
        }

        /// <summary>
        /// Triangle rotation
        /// </summary>
        /// <param name="degrees"></param>
        public void Rotate(int degrees)
        {
            float angle = (float)(degrees / 360f * Math.PI);
            float newX = (float)(A.X * Math.Cos(angle) - A.Y * Math.Sin(angle));
            float newY = (float)(A.X * Math.Sin(angle) + A.Y * Math.Cos(angle));

            AX = newX;
            AZ = newY;

            newX = (float)(B.X * Math.Cos(angle) - B.Y * Math.Sin(angle));
            newY = (float)(B.X * Math.Sin(angle) + B.Y * Math.Cos(angle));

            B.X = newX;
            BY = newY;

            newX = (float)(C.X * Math.Cos(angle) - C.Y * Math.Sin(angle));
            newY = (float)(C.X * Math.Sin(angle) + C.Y * Math.Cos(angle));

            CX = newX;
            CY = newY;
        }
    }
}

form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Triangle t;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PointF A = new PointF(0, -10);
            PointF B = new PointF(10, 10);
            PointF C = new PointF(-10, 10);

            t = new Triangle(A, B, C);
            Graphics g = panel1.CreateGraphics();
            g.TranslateTransform(50, 50);
            t.Draw(g);
        }
    }
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325480331&siteId=291194637