博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asp.Net验证码2
阅读量:6816 次
发布时间:2019-06-26

本文共 4478 字,大约阅读时间需要 14 分钟。

using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Drawing;using System.IO;using System.Drawing.Drawing2D;using System.Drawing.Imaging;public partial class user_Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        string checkCode = CreateRandomCode(4);        GetImgWithValidateCode(checkCode, 132, 35,20);    }    ///    /// 生成随机字符串    ///    ///字符数量    ///
随机字符串
private string CreateRandomCode(int codeCount) { string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z"; string[] allCharArray = allChar.Split(','); string randomCode = ""; int temp = -1; Random rand = new Random(); for (int i = 0; i < codeCount; i++) { if (temp != -1) { rand = new Random(i * temp * ((int)DateTime.Now.Ticks)); } int t = rand.Next(35); if (temp == t) { return CreateRandomCode(codeCount); } temp = t; randomCode += allCharArray[t]; } return randomCode; } /// /// 创建验证码图片 /// ///字符串 ///宽 ///高 ///字号 public void GetImgWithValidateCode(string code, int width, int height, int fontSize) { Color bgcolor = GetControllableColor(150); Color color = GetControllableColor(100); int charNum = code.Length; Bitmap bitMap = null; Graphics gph = null; //创建内存流 MemoryStream memStream = new MemoryStream(); Random random = new Random(); //创建位图对象 bitMap = new Bitmap(width, height); //根据上面创建的位图对象创建绘图图面 gph = Graphics.FromImage(bitMap); //设定验证码图片背景色 gph.Clear(bgcolor); //产生随机干扰线条 for (int i = 0; i < 1; i++) { Pen backPen = new Pen(color, 2); int x = 0; int y = height / 2; int x2 = width; int y2 = random.Next(height); gph.DrawLine(backPen, x, y, x2, y2); } SolidBrush sb = new SolidBrush(color); PointF Cpoint1 = new PointF(5, 5); Random rnd1 = new Random(); int x1 = 0, y1 = 0; //通过循环,绘制每个字符, for (int i = 0; i < code.Length; i++) { x1 = rnd1.Next(2) + ((width - 10) / code.Length) * i; y1 = rnd1.Next(bitMap.Height / 4); Cpoint1 = new PointF(x1, y1); Font textFont = new Font("Arial", fontSize, FontStyle.Bold);//字体随机,字号大小30,加粗 //随机倾斜字符 Matrix transform = gph.Transform; transform.Shear(Convert.ToSingle(rnd1.NextDouble() - 0.5), 0.001f); gph.Transform = transform; gph.DrawString(code.Substring(i, 1), textFont, sb, Cpoint1); gph.ResetTransform(); } //画图片的前景噪音点 for (int i = 0; i < 10; i++) { int x = random.Next(bitMap.Width); int y = random.Next(bitMap.Height); bitMap.SetPixel(x, y, Color.White); } //画图片的边框线 gph.DrawRectangle(new Pen(Color.Black, 2), 0, 0, bitMap.Width - 1, bitMap.Height - 1); try { bitMap.Save(memStream, ImageFormat.Gif); } catch (Exception ex) { Console.WriteLine(ex.Message); } bitMap.Dispose(); System.Drawing.Image img = System.Drawing.Image.FromStream(memStream); gph.DrawImage(img, 50, 20, width, height); //img.Save(@"D:\test.jpg", ImageFormat.Jpeg); //如果是asp.net 用下面的输出图片 System.IO.MemoryStream ms = new System.IO.MemoryStream(); img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ClearContent(); Response.ContentType = "image/Jpeg"; Response.BinaryWrite(ms.ToArray()); gph.Dispose(); img.Dispose(); } /// /// 产生一种 R,G,B 均大于 colorBase 随机颜色,以确保颜色不会过深 /// ///The color base. ///
Color
public Color GetControllableColor(int colorBase) { Color color = Color.Black; if (colorBase > 200) { return color; } Random random = new Random(); color = Color.FromArgb(random.Next(56) + colorBase, random.Next(56) + colorBase, random.Next(56) + colorBase); return color; }}

验证码效果:

还有2种验证码请看我博客

http://www.cnblogs.com/webapi/p/5725576.html

http://www.cnblogs.com/webapi/p/5726498.html

你可能感兴趣的文章
MySQL5.7使用Notifier启动、停止服务时出现的问题
查看>>
今天用java弄个json数据交换接口,个人感觉这样实现省事实力。
查看>>
5 Servlet
查看>>
百度创始人李彦宏:要做最好的中文搜索引擎
查看>>
3.26作业
查看>>
Python里的append和extend
查看>>
cut命令
查看>>
JavaScript强化教程-cookie对象
查看>>
MEMCACHE常用的命令
查看>>
docker 基础
查看>>
Angular基础(七) HTTP & Routing
查看>>
使用Freeline提高你的工作效率
查看>>
FTP服务器
查看>>
爬百度新闻
查看>>
TCP协议与UDP协议的区别
查看>>
软件定时器算法
查看>>
VB.NET 自动打包程序
查看>>
CISCO引擎RPR SSO
查看>>
LINUX APACHE 安装测试
查看>>
Java导致登录UCS Manager异常
查看>>