偶的小收获(关于验证码随机产生图片)
2011-08-12 16:19
393 查看
今日偶看mvc设计模式,看见产生一个验证码的生成原理,觉得不错,记下来,空以后有用。
1:前期产生随机的目标字符代码
Create Img Code
/// <summary> /// 创建验证码的图片 /// </summary> /// <param name="containsPage">要输出到的page对象</param> /// <param name="validateNum">验证码</param> public byte[] CreateValidateGraphic(string validateCode) { Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 23.0), 40); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的干扰线 for (int i = 0; i < 25; 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.Silver), x1, y1, x2, y2); } Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString(validateCode, font, brush, 22, 10); //画图片的前景干扰点 for (int i = 0; i < 80; 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); //保存图片数据 MemoryStream stream = new MemoryStream(); image.Save(stream, ImageFormat.Jpeg); //输出图片流 return stream.ToArray(); } finally { g.Dispose(); image.Dispose(); } }
相关文章推荐
- python在图片写文字,产生随机验证码序列号
- 一个产生随机图片验证码的示例
- 重写IHttpHandler接口实现产生随机验证码图片
- wed网页随机产生验证码图片(版本tomcat-8.0.14)
- 【wed】wed网页随机产生验证码图片(版本tomcat-8.0.14)
- 随机产生验证码图片---参考代码
- 一个产生随机图片验证码的示例
- WEB Service产生随机验证码图片
- 【wed】wed网页随机产生验证码图片(版本tomcat-8.0.14)
- Android菜鸟练习第十五课 随机生成验证码及验证码图片
- 关于一道 产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复
- python--random模块(产生随机值)、洗牌、验证码应用
- 随机产生验证码
- 关于URLDownlaodtoFile()函数下载的验证码图片都一样的问题的解决
- MVC把随机产生的字符串转换为图片
- 模拟地图撒点,将随机产生的一些点以圆的形式画在画布上并保存为png格式的图片
- 随机产生验证码
- ASP.NET产生随机验证码
- 产生验证码图片
- ASP.NET随机生成验证码图片