图片验证码
{
//Random r = new Random();
//string s = r.Next(10000, 99999).ToString();
Random r = new Random();//创建一个随机数对象
int sa = r.Next(65, 123);//创建一个A-z为的随机数
byte[] saa = new byte[] { Convert.ToByte(sa) };
string s = System.Text.ASCIIEncoding.UTF8.GetString(saa);
r = new Random();
sa = r.Next(66, 123);
saa = new byte[] { Convert.ToByte(sa) };
s += System.Text.ASCIIEncoding.UTF8.GetString(saa);
r = new Random();
sa = r.Next(77, 123);
saa = new byte[] { Convert.ToByte(sa) };
s += System.Text.ASCIIEncoding.UTF8.GetString(saa);
r = new Random();
sa = r.Next(98, 123);
saa = new byte[] { Convert.ToByte(sa) };
s += System.Text.ASCIIEncoding.UTF8.GetString(saa);
System.Drawing.Image image = new Bitmap(50, 18);
Graphics gh = Graphics.FromImage(image); //根据图片对象创建一个绘图对象
gh.DrawString(s, new Font("宋体", 12), Brushes.Yellow, new PointF(3, 0)); //绘制一个文字到图片对象,在方法参数中,s为要绘制的文字,Font为字体和大小,Brushes为色彩,PointF为绘制的坐标
gh.DrawLine(Pens.White,new Point(0,18),new Point(50,8)); //在图片上绘制线
gh.DrawLine(Pens.White, new Point(0, 4), new Point(40, 1)); //在图片上绘制线
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); //将图片对象保存为Response.OutputStream(服务器端向客户端浏览器响应的输出流对象),保存格式为ImageFormat.Jpeg。
Response.End();//结束响应。
}
声明:本人笔记 请各位大虾多多指教