.Net验证码实现基础--Draw

命名空间

using System.Draw;

using System.Draw.Drawing2D;

在form等控件的 事件中 添加 paint事件

///////画各种形状(空心)///////

e.Graphics.Clear(Color.AliceBlue);//清楚整个绘画面并以制定的颜色(这里是爱丽丝蓝--Color.AliceBlue)填充

e.Graphics.DrawArc();//画弧线

e.Graphics.DrawCurve();//不闭合曲线

e.Graphics.DrawClosedCurve();//闭合的曲线

e.Graphics.DrawEllipse(pen, 80, 80, 150, 150);//画一个椭圆--如果宽和高相等则是一个正圆

Image img = Image.FromFile("clumsy-smurf-icon.png");//通过图片名称获得图片
e.Graphics.DrawImage(img,20,20);//画图片

e.Graphics.DrawRectangle();//画矩形 

//画文字

string s = "红鲤鱼与绿鲤鱼与驴";
Font font = new System.Drawing.Font("隶书",18);
SolidBrush brush = new SolidBrush(Color.Cyan);
e.Graphics.DrawString(s, font, brush,200,50);

//画渐变色文字

string s = "红鲤鱼与绿鲤鱼与驴";
Font font = new System.Drawing.Font("隶书",18);
SolidBrush brush1 = new SolidBrush(Color.Cyan);//常规颜色
Point point1 = new Point(0,0);//起始点1与点2之间距离越近变换的越频繁
Point point2 = new Point(20,40);
LinearGradientBrush brush2 = new LinearGradientBrush(point1, point2, Color.DarkCyan, Color.DeepPink);//渐变色
e.Graphics.DrawString(s, font, brush2,200,50);

实例:验证码

 1         private void pictureBox1_Paint(object sender, PaintEventArgs e)//装验证码的picturebox
 2         {
 3             _IdentifyingCode = "";
 4             string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQISTUVWXYZ0123456789";
 5             Random rand = new Random();
 6             for (int i = 0; i < 4; i++)//截取四位验证码
 7             {
 8                 int start = rand.Next(str.Length);
 9                 string s = str.Substring(start, 1);
10                 _IdentifyingCode += s;
11             }
12             //渐变色
13             Point startpoint = new Point(0, 0);
14             Point endpoint = new Point(80, 45);
15             LinearGradientBrush brush = new LinearGradientBrush(startpoint, endpoint, Color.LightGreen, Color.Yellow);
16             //SolidBrush brush = new System.Drawing.SolidBrush(Color.Chocolate);//实线
17             Font font = new System.Drawing.Font("Buxton Sketch",16);
18             e.Graphics.DrawString(_IdentifyingCode, font, brush, 5, 12);
19         }

////////画各种形状(实心)///////

同画空心图形把代码中的Draw改为Fill,如:e.Graphics.FillPie();//画一个实心扇形

原文地址:https://www.cnblogs.com/liujiangping/p/4676129.html