博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
随机生成验证码
阅读量:5844 次
发布时间:2019-06-18

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

1】

1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Collections; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.WebControls; 9 using System.Web.UI.WebControls.WebParts;10 using System.Web.UI.HtmlControls;11 using System.Drawing;12 13 public partial class checkcode : System.Web.UI.Page14 {15     protected void Page_Load(object sender, EventArgs e)16     {17         CreateCheckCodeImage(GenerateCheckCode());18     }19     private string GenerateCheckCode()20     {21         int number;22         char code;23         string checkCode = String.Empty;24         System.Random random = new Random();25 26         for (int i = 0; i < 4; i++)27         {28             number = random.Next();29 30             if (number % 2 == 0)31                 code = (char)('0' + (char)(number % 10));32             else33                 code = (char)('A' + (char)(number % 26));34 35             checkCode += code.ToString();36         }37         // 随机生成的字母或数字组合放入Cookie中,则可以通过Cookie取得验证码,查看输入的是否匹配38         Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));39         return checkCode;40     }41     private void CreateCheckCodeImage(string checkCode)42     {43         if (checkCode == null || checkCode.Trim() == String.Empty)44             return;45         System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);46         Graphics g = Graphics.FromImage(image);47         try48         {49             //生成随机生成器50             Random random = new Random();51             //清空图片背景色52             g.Clear(Color.White);53             //画图片的背景噪音线54             for (int i = 0; i < 2; i++)55             {56                 int x1 = random.Next(image.Width);57                 int x2 = random.Next(image.Width);58                 int y1 = random.Next(image.Height);59                 int y2 = random.Next(image.Height);60                 g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);61             }62             Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));63             System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), 64                                                                                                                   Color.Blue, Color.DarkRed, 1.2f, true);65             g.DrawString(checkCode, font, brush, 2, 2);66             //画图片的前景噪音点67             for (int i = 0; i < 100; i++)68             {69                 int x = random.Next(image.Width);70                 int y = random.Next(image.Height);71                 image.SetPixel(x, y, Color.FromArgb(random.Next()));72             }73             //画图片的边框线74             g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);75             System.IO.MemoryStream ms = new System.IO.MemoryStream();76             image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);77             Response.ClearContent();78             Response.ContentType = "image/Gif";79             Response.BinaryWrite(ms.ToArray());80         }81         finally82         {83             g.Dispose();84             image.Dispose();85         }86     }87 }
View Code

2】

1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Security; 8 using System.Web.UI; 9 using System.Web.UI.HtmlControls;10 using System.Web.UI.WebControls;11 using System.Web.UI.WebControls.WebParts;12 using System.Xml.Linq;13 using System.Drawing;14 using System.Drawing.Imaging;15 using System.Drawing.Drawing2D;16 17 namespace Web18 {19     public partial class yanzheng : System.Web.UI.Page20     {21         protected void Page_Load(object sender, EventArgs e)22         {23             CreateCheckCodeImage(GenCode(7));24         }25         /**/26         /// 27         /// '产生随机字符串28         /// 29         /// 随机出几个字符30         /// 
随机出的字符串
31 private string GenCode(int num)32 {33 string[] source = { "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", "V", "W", "X", "Y", "Z" };34 string code = "";35 Random rd = new Random();36 int i;37 for (i = 0; i < num; i++)38 {39 code += source[rd.Next(0, source.Length)];40 }41 return code;42 43 }44 45 /**/46 /// 47 /// 生成图片(增加背景噪音线、前景噪音点)48 /// 49 /// 随机出字符串50 private void CreateCheckCodeImage(string checkCode)51 {52 if (checkCode.Trim() == "" || checkCode == null)53 return;54 Session["Code"] = checkCode; //将字符串保存到Session中,以便需要时进行验证55 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)(checkCode.Length * 15), 22);56 Graphics g = Graphics.FromImage(image);57 try58 {59 //生成随机生成器60 Random random = new Random();61 62 //清空图片背景色63 g.Clear(Color.White);64 65 // 画图片的背景噪音线66 int i;67 for (i = 0; i < 30; i++)68 {69 int x1 = random.Next(image.Width);70 int x2 = random.Next(image.Width);71 int y1 = random.Next(image.Height);72 int y2 = random.Next(image.Height);73 g.DrawLine(new Pen(Color.Gainsboro), x1, y1, x2, y2);//绘制一条连接由坐标对指定的两个点的线条74 }75 76 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));77 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.MidnightBlue, Color.Green, 1.2F, true);78 g.DrawString(checkCode, font, brush, 2, 2);79 80 //画图片的前景噪音点81 g.DrawRectangle(new Pen(Color.YellowGreen), 0, 0, image.Width - 1, image.Height - 1);82 83 //输出84 System.IO.MemoryStream ms = new System.IO.MemoryStream();85 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);86 Response.ClearContent();87 Response.ContentType = "image/Gif";88 Response.BinaryWrite(ms.ToArray());89 90 }91 catch92 {93 g.Dispose();94 image.Dispose();95 }96 97 }98 }99 }
View Code

 

 

 

 

 

 

 

转载地址:http://quqcx.baihongyu.com/

你可能感兴趣的文章
浮点数内存如何存储的
查看>>
问题账户需求分析
查看>>
hp 服务器通过串口重定向功能的使用
查看>>
java中回调函数以及关于包装类的Demo
查看>>
ul下的li浮动,如何是ul有li的高度
查看>>
网站文章如何能自动判定是抄袭?一种算法和实践架构剖析
查看>>
【OpenCV学习】滚动条
查看>>
为找好心人抚养孩子 浙江一离婚父亲将幼童丢弃公园
查看>>
读书:为了那个美妙的咔哒声
查看>>
ASMFD (ASM Filter Driver) Support on OS Platforms (Certification Matrix). (文档 ID 2034681.1)
查看>>
OllyDBG 入门系列教学--让你瞬间成为破解高手
查看>>
listbox用法
查看>>
寻找链表相交节点
查看>>
Linux crontab定时执行任务
查看>>
自己遇到的,曾未知道的知识点
查看>>
docker 基础
查看>>
我的友情链接
查看>>
写Use Case的一种方式,从oracle的tutorial抄来的
查看>>
【C#】protected 变量类型
查看>>
爬虫去重(只是讲了去重的策略,没有具体讲实现过程,反正就是云里雾里)...
查看>>