基本图形绘制

1、绘制直线

两点确定一条直线,采用Graphics类中的DrawLine方法

        private void button1_Click(object sender, EventArgs e)
        {
            Pen blackPen = new Pen(Color.Black, 3);//实例化Pen类
            Point point1 = new Point(10, 50);//实例化一个Point类
            Point point2 = new Point(100, 50);//再实例化一个Point类
            Graphics g = this.CreateGraphics();//实例化一个Graphics类
            g.DrawLine(blackPen, point1, point2);//调用DrawLine方法绘制直线
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics graphics = this.CreateGraphics();//实例化Graphics类
            Pen myPen = new Pen(Color.Black, 3);//实例化Pen类
            graphics.DrawLine(myPen, 150, 30, 150, 100);//调用DrawLine方法绘制直线
        }

2、绘制矩形,采用Graphics类中的DrawRectangle方法

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics graphics = this.CreateGraphics();//声明一个Graphics对象
            Pen myPen = new Pen(Color.Blue, 8);//实例化Pen类
            //调用Graphics对象的DrawRectangle方法
            graphics.DrawRectangle(myPen, 10, 10, 150, 100);
        }

3、绘制正方形,采用Graphics类中的DrawRecentangle方法,将宽和高设置相同即可

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics graphics = this.CreateGraphics();//声明一个Graphics对象
            Pen myPen = new Pen(Color.Blue, 8);//实例化Pen类
            //调用Graphics对象的DrawRectangle方法
            graphics.DrawRectangle(myPen, 30, 10, 100, 100);
        }

4、绘制椭圆,采用Graphics类中的DrawEllipse方法

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics graphics = this.CreateGraphics();//创建Graphics对象
            Pen myPen = new Pen(Color.DarkOrange, 3);//创建Pen对象
            graphics.DrawEllipse(myPen, 55, 20, 100, 50);//绘制椭圆
        }

5、绘制指定弧度的扇形,采用Graphics类中的DrawPie方法

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics ghs = this.CreateGraphics();//实例化Graphics类
            Pen myPen = new Pen(Color.DarkRed, 3);//实例化Pen类
            Rectangle myRectangle = new Rectangle(50, 20, 100, 60);//定义一个Rectangle结构
            ghs.DrawArc(myPen, myRectangle, 180, 180);//绘制圆弧
        }

6、绘制贝尔曲线,采用Graphics类中的DrawBezier方法

         private void button1_Click(object sender, EventArgs e)
        {
            Graphics ghs = this.CreateGraphics();//实例化Graphics类
            Pen mypen = new Pen(Color.Black, 3);//实例化Pen类
            ghs.DrawPie(mypen, 20, 10, 120, 100, 210, 120);//绘制扇形
        }

7、绘制多边形,采用Graphics类中的DrawPolygon方法

       private void Draw_Click(object sender, EventArgs e)
        {
            Graphics graphics = this.CreateGraphics();//实例化一个GDI+绘图图面类对象
            Pen myPen = new Pen(Color.Blue, 3);//实例化一个用于绘制直线和曲线的对象
            float startX = 50.0F;//实例化起始点的x坐标
            float startY = 80.0F;//实例化起始点的y坐标
            float controlX1 = 200.0F;//实例化第一个控制点的x坐标
            float controlY1 = 20.0F;//实例化第一个控制点的y坐标
            float controlX2 = 100.0F;//实例化第二个控制点的x坐标
            float controlY2 = 10.0F;//实例化第二个控制点的y坐标
            float endX = 190.0F;//实例化结束点的x坐标
            float endY = 40.0F;//实例化结束点的y坐标
            graphics.DrawBezier(myPen, startX, startY, controlX1, controlY1, controlX2, controlY2, endX, endY);//绘制由4个表示点的有序坐标对定义的贝塞尔样条
        }

8、绘制文本,采用Draphics类中的DrawString方法

         private void button1_Click(object sender, EventArgs e)
        {
            string str = "明日科技 C#编程词典";//定义绘制的字符串
            Font myFont = new Font("华文行楷", 20);//实例化Font对象
            SolidBrush myBrush = new SolidBrush(Color.DarkOrange);//实例化画刷对象
            Graphics myGraphics = this.CreateGraphics();//创建Graphics对象
            myGraphics.DrawString(str, myFont, myBrush,10,20);//绘制文本
        }

9、绘制公章

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;
using System.Drawing.Drawing2D;

namespace Cachet
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        Font Var_Font = new Font("Arial", 12, FontStyle.Bold);//定义字符串的字体样式
        Rectangle rect = new Rectangle(10, 10, 160, 160);//实例化Rectangle类

        private void button1_Click(object sender, EventArgs e)
        {
            int tem_Line = 0;//记录圆的直径
            int circularity_W = 4;//设置圆画笔的粗细
            if (panel1.Width >= panel1.Height)//如果panel1控件的宽度大于等于高度
                tem_Line = panel1.Height;//设置高度为圆的直径
            else
                tem_Line = panel1.Width;//设置宽度为圆的直径
            rect = new Rectangle(circularity_W, circularity_W, tem_Line - circularity_W * 2, tem_Line - circularity_W * 2);//设置圆的绘制区域
            Font star_Font = new Font("Arial", 30, FontStyle.Regular);//设置星号的字体样式
            string star_Str = "";
            Graphics g = this.panel1.CreateGraphics();//实例化Graphics类
            g.SmoothingMode = SmoothingMode.AntiAlias;//消除绘制图形的锯齿
            g.Clear(Color.White);//以白色清空panel1控件的背景
            Pen myPen = new Pen(Color.Red, circularity_W);//设置画笔的颜色
            g.DrawEllipse(myPen, rect); //绘制圆 
            SizeF Var_Size = new SizeF(rect.Width, rect.Width);//实例化SizeF类
            Var_Size = g.MeasureString(star_Str, star_Font);//对指定字符串进行测量
            //要指定的位置绘制星号
            g.DrawString(star_Str, star_Font, myPen.Brush, new PointF((rect.Width / 2F) + circularity_W - Var_Size.Width / 2F, rect.Height / 2F - Var_Size.Width / 2F));
            Var_Size = g.MeasureString("专用章", Var_Font);//对指定字符串进行测量
            //绘制文字
            g.DrawString("专用章", Var_Font, myPen.Brush, new PointF((rect.Width / 2F) + circularity_W - Var_Size.Width / 2F, rect.Height / 2F + Var_Size.Height * 2));
            string tempStr = "吉林省明日科技有限公司";
            int len = tempStr.Length;//获取字符串的长度
            float angle = 180 + (180 - len * 20) / 2;//设置文字的旋转角度
            for (int i = 0; i < len; i++)//将文字以指定的弧度进行绘制
            {
                //将指定的平移添加到g的变换矩阵前         
                g.TranslateTransform((tem_Line + circularity_W / 2) / 2, (tem_Line + circularity_W / 2) / 2);
                g.RotateTransform(angle);//将指定的旋转用于g的变换矩阵   
                Brush myBrush = Brushes.Red;//定义画刷
                g.DrawString(tempStr.Substring(i, 1), Var_Font, myBrush, 60, 0);//显示旋转文字
                g.ResetTransform();//将g的全局变换矩阵重置为单位矩阵
                angle += 20;//设置下一个文字的角度
            }
        }
    }
}

10、波形图的绘制

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics graphics = this.CreateGraphics();                    //实例化窗体的Graphics类
            Pen myPen = new Pen(Color.Black, 1);                        //设置画笔
            int beginX = 50;                                        //定义变量
            int beginY = 65;
            int height = 35;
            int width = 50;
            Point pointX1 = new Point(beginX, beginY);
            Point pointY1 = new Point(beginX + 210, beginY);
            Point pointX2 = new Point(beginX, beginY - 45);
            Point pointY2 = new Point(beginX, beginY + 45);
            //调用DrawLine方法绘制两条垂直相交的直线,用来作为波形图的横纵坐标
            graphics.DrawLine(myPen, pointX1, pointY1);
            graphics.DrawLine(myPen, pointX2, pointY2);
            graphics.DrawBezier(myPen, beginX, beginY, beginX + 15, beginY - height, beginX + 40, beginY - height, beginX + width,
            beginY);                                             //绘制上半区域交错连接的贝塞尔曲线
            graphics.DrawBezier(myPen, beginX + width, beginY, beginX + width + 15, beginY + height, beginX + width + 40,
        beginY + height, beginX + width * 2, beginY);                     //绘制下半区域交错连接的贝塞尔曲线
            graphics.DrawBezier(myPen, beginX + width * 2, beginY, beginX + width * 2 + 15, beginY - height, beginX + width * 2
        + 40, beginY - height, beginX + width * 3, beginY);                     //绘制上半区域交错连接的贝塞尔曲线
            graphics.DrawBezier(myPen, beginX + width * 3, beginY, beginX + width * 3 + 15, beginY + height, beginX + width * 3
        + 40, beginY + height, beginX + width * 4, beginY);                 //绘制下半区域交错连接的贝塞尔曲线
        }

11、绘制图形验证码

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 DrawValidateCode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CodeImage(CheckCode());
        }

        private string CheckCode()                                //此方法生成
        {
            int number;
            char code;
            string checkCode = String.Empty;                    //声明变量存储随机生成的4位英文或数字
            Random random = new Random();                        //生成随机数
            for (int i = 0; i < 4; i++)
            {
                number = random.Next();                            //返回非负随机数
                if (number % 2 == 0)                            //判断数字是否为偶数
                    code = (char)('0' + (char)(number % 10));
                else                                            //如果不是偶数
                    code = (char)('A' + (char)(number % 26));
                checkCode += " " + code.ToString();                //累加字符串
            }
            return checkCode;                                    //返回生成的字符串
        }

        private void CodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;
            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 9.5)), 22);
            Graphics g = Graphics.FromImage(image);                    //创建Graphics对象
            try
            {
                Random random = new Random();                        //生成随机生成器
                g.Clear(Color.White);                                 //清空图片背景色
                for (int i = 0; i < 3; i++)                            //画图片的背景噪音线
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
                }
                Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
                g.DrawString(checkCode, font, new SolidBrush(Color.Red), 2, 2);
                for (int i = 0; i < 150; i++)                        //画图片的前景噪音点
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                this.pictureBox1.Width = image.Width;                //设置PictureBox的宽度
                this.pictureBox1.Height = image.Height;                //设置PictureBox的高度
                this.pictureBox1.BackgroundImage = image;            //设置PictureBox的背景图像
            }
            catch
            { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CodeImage(CheckCode());

        }
    }
}

12、绘制中文验证码

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;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace ChineseCode
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }
        public string txt = "";
        private void Form1_Load(object sender, EventArgs e)
        {
            CreateImage();
        }
        private void CreateImage()
        {
            //获取GB2312编码页(表) 
            Encoding gb = Encoding.GetEncoding("gb2312");
            //调用函数产生4个随机中文汉字编码 
            object[] bytes = CreateCode(4);
            //根据汉字编码的字节数组解码出中文汉字 
            string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
            string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
            string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
            string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
            txt = str1 + str2 + str3 + str4;
            if (txt == null || txt == String.Empty)
            {
                return;
            }
            Bitmap image = new Bitmap((int)Math.Ceiling((txt.Length * 20.5)), 22);
            Graphics g = Graphics.FromImage(image);
            try
            {
                //生成随机生成器
                Random random = new Random();
                //清空图片背景色
                g.Clear(Color.White);
                //画图片的背景噪音线
                for (int i = 0; i < 2; i++)
                {
                    Point tem_Point_1 = new Point(random.Next(image.Width), random.Next(image.Height));
                    Point tem_Point_2 = new Point(random.Next(image.Width), random.Next(image.Height));
                    g.DrawLine(new Pen(Color.Black), tem_Point_1, tem_Point_2);
                }
                Font font = new Font("宋体", 12, (FontStyle.Bold));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(txt, font, brush, 2, 2);
                //画图片的前景噪音点
                for (int i = 0; i < 100; i++)
                {
                    Point tem_point = new Point(random.Next(image.Width),random.Next(image.Height));
                    image.SetPixel(tem_point.X,tem_point.Y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                pictureBox1.Image = image;
            }
            catch { }
        }

        /**/
        /* 
        此函数在汉字编码范围内随机创建含两个元素的十六进制字节数组,每个字节数组代表一个汉字,并将 
        四个字节数组存储在object数组中。 
        参数:strlength,代表需要产生的汉字个数 
        */
        public static object[] CreateCode(int strlength)
        { 
            //定义一个字符串数组储存汉字编码的组成元素 
            string[] r=new String [16]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; 
            Random rnd=new Random(); 
            //定义一个object数组用来 
            object[] bytes=new object[strlength]; 
            /**//*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中 
             每个汉字有四个区位码组成 
             区位码第1位和区位码第2位作为字节数组第一个元素 
             区位码第3位和区位码第4位作为字节数组第二个元素 
            */ 
            for(int i=0;i<strlength;i++) 
            { 
                //区位码第1位 
                int r1=rnd.Next(11,14);
                string str_r1 = r[r1].Trim(); 
                //区位码第2位 
                rnd=new Random(r1*unchecked((int)DateTime.Now.Ticks)+i);//更换随机数发生器的种子避免产生重复值 
                int r2; 
                if (r1==13) 
                    r2=rnd.Next(0,7); 
                else 
                    r2=rnd.Next(0,16); 
                string str_r2 = r[r2].Trim(); 
                //区位码第3位 
                rnd=new Random(r2*unchecked((int)DateTime.Now.Ticks)+i); 
                int r3=rnd.Next(10,16);
                string str_r3 = r[r3].Trim(); 
                //区位码第4位 
                rnd=new Random(r3*unchecked((int)DateTime.Now.Ticks)+i); 
                int r4; 
                if (r3==10) 
                { 
                    r4=rnd.Next(1,16); 
                } 
                else if (r3==15) 
                { 
                    r4=rnd.Next(0,15); 
                } 
                else 
                { 
                    r4=rnd.Next(0,16); 
                }
                string str_r4 = r[r4].Trim(); 
                //定义两个字节变量存储产生的随机汉字区位码 
                byte byte1=Convert.ToByte(str_r1 + str_r2,16); 
                byte byte2=Convert.ToByte(str_r3 + str_r4,16); 
                //将两个字节变量存储在字节数组中 
                byte[] str_r=new byte[]{byte1,byte2}; 
                //将产生的一个汉字的字节数组放入object数组中 
                bytes.SetValue(str_r,i);    
            } 
            return bytes; 
       }

        private void button2_Click(object sender, EventArgs e)
        {
            CreateImage();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtCode.Text.Trim() == "")
                return;
            else
            {
                if (txtCode.Text.Trim() == txt)
                {
                    MessageBox.Show("提示:验证码输入正确!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("提示:验证码输入错误,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        } 
    }
}

 Bitmap类是封装GDI+位图,此位图由图形图像及其特性的像素数据组成,其实用于处理由像素数据定义的图像的对象,位于System.Drawing命名空间下,引用System.Drawing.dll程序集

原文地址:https://www.cnblogs.com/chenyongblog/p/3293358.html