本文主要是介绍ASP.NET C# 验证码 支持中文 噪点 弯曲,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/// <summary>
/// 根据指定参数返回BitMap对象
/// 引用如下:
/// using System.Drawing;
/// 调用例子如下:
/// eg1、保存为图象文件为
/// Bitmap srBmp = srBitmap(srs);
/// srBmp.Save(Directory.GetCurrentDirectory() + "//srs.gif", System.Drawing.Imaging.ImageFormat.Gif);
/// srBmp.Dispose();
/// eg2。网页中调用方式如下
/// Bitmap srBmp = srBitmap(srs);
/// System.IO.MemoryStream srMS = new System.IO.MemoryStream();
/// srBmp.Save(srMS,System.Drawing.Imaging.ImageFormat.Gif);
/// Response.ClearContent();
/// Response.ContentType = "image/gif";
/// Response.BinaryWrite(srMS.ToArray());
/// srBmp.Dispose();
/// </summary>
/// <param name="srs"></param>
/// <returns></returns>
public static Bitmap srBitmap(string srs)
{
//定义图片弯曲的角度
int srseedangle = 45;
//定义图象
Bitmap srBmp = new Bitmap(srs.Length*20,30);
//画图
Graphics srGraph = Graphics.FromImage(srBmp);
//清空图象
srGraph.Clear(Color.AliceBlue);
//给图象画边框
srGraph.DrawRectangle(new Pen(Color.Black,0),0,0,srBmp.Width-1,srBmp.Height-1);
//定义随即数
Random srRandom = new Random();
//定义画笔
Pen srPen = new Pen(Color.LightGray,0);
//画噪点
for (int i = 0; i < 100; i++)
{
srGraph.DrawRectangle(srPen,srRandom.Next(1,srBmp.Width-2),srRandom.Next(1,srBmp.Height-2),1,1);
}
//将字符串转化为字符数组
char[] srchars = srs.ToCharArray();
//封状文本
StringFormat srFormat = new StringFormat(StringFormatFlags.NoClip);
//设置文本垂直居中
srFormat.Alignment = StringAlignment.Center;
//设置文本水平居中
srFormat.LineAlignment = StringAlignment.Center;
//定义字体颜色
Color[] srColors ={ Color.Black,Color.Red,Color.DarkBlue,Color.Blue,Color.Orange,Color.Brown,Color.DarkCyan,Color.Purple};
//定义字体
string[] srFonts ={ "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
//循环画出每个字符
for (int i = 0, j = srchars.Length; i < j; i++)
{
//定义字体 参数分别为字体样式 字体大小 字体字形
Font srFont = new Font(srFonts[srRandom.Next(5)], srRandom.Next(12,20), FontStyle.Regular);
//填充图形
Brush srBrush = new SolidBrush(srColors[srRandom.Next(7)]);
//定义坐标
Point srPoint = new Point(16, 16);
//定义倾斜角度
float srangle = srRandom.Next(-srseedangle, srseedangle);
//倾斜
srGraph.TranslateTransform(srPoint.X, srPoint.Y);
srGraph.RotateTransform(srangle);
//填充字符
srGraph.DrawString(srchars[i].ToString(),srFont,srBrush,1,1,srFormat);
//回归正常
srGraph.RotateTransform(-srangle);
srGraph.TranslateTransform(2,-srPoint.Y);
}
srGraph.Dispose();
return srBmp;
}
这篇关于ASP.NET C# 验证码 支持中文 噪点 弯曲的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!