asp.net生成图片验证码(数字)
来源:互联网 发布:bt种子在线播放软件 编辑:程序博客网 时间:2024/06/03 22:48
前台用一个img来装
<img src="../png.aspx" id="img" onclick="f_refreshtype()" />
注意路径直接写你后台aspx文件
js实现点击刷新
function f_refreshtype() { var Image1 = document.getElementById("img"); if (Image1 != null) { Image1.src = Image1.src + "?"; } }
后台用一个aspx文件生成验证码图片(4位数字)
using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;using System.IO;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace ChemGuanWeb{ public partial class png : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { CreateCheckCodeImage(GenerateCheckCodes(4)); } } public void ShowAuthCode(Stream stream, out string code) { Random random = new Random(); code = random.Next(1000, 9999).ToString(); Bitmap bitmap = CreateAuthCode(code); bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Gif); } private string GenerateCheckCodes(int iCount) { int number; string checkCode = String.Empty; int iSeed = DateTime.Now.Millisecond; System.Random random = new Random(iSeed); for (int i = 0; i < iCount; i++) { number = random.Next(10); checkCode += number.ToString(); } Session["CheckCode"] = checkCode; return checkCode; } private Bitmap CreateAuthCode(string str) { Font fn = new Font("宋体", 12); Brush forecolor = Brushes.Black; Brush bgcolor = Brushes.White; PointF pf = new PointF(5, 5); Bitmap bitmap = new Bitmap(100, 25); Rectangle rec = new Rectangle(0, 0, 100, 25); Graphics gh = Graphics.FromImage(bitmap); gh.FillRectangle(bgcolor, rec); gh.DrawString(str, fn, forecolor, pf); return bitmap; } private void CreateCheckCodeImage(string checkCode) { if (checkCode == null || checkCode.Trim() == String.Empty) return; int iWordWidth = 15; int iImageWidth = checkCode.Length * iWordWidth; Bitmap image = new Bitmap(iImageWidth, 20); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的背景噪音点 for (int i = 0; i < 20; 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); } //画图片的背景噪音线 for (int i = 0; i < 2; i++) { int x1 = 0; int x2 = image.Width; int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); if (i == 0) { g.DrawLine(new Pen(Color.Gray, 2), x1, y1, x2, y2); } } for (int i = 0; i < checkCode.Length; i++) { string Code = checkCode[i].ToString(); int xLeft = iWordWidth * (i); random = new Random(xLeft); int iSeed = DateTime.Now.Millisecond; int iValue = random.Next(iSeed) % 4; if (iValue == 0) { Font font = new Font("Arial", 13, (FontStyle.Bold | System.Drawing.FontStyle.Italic)); Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height); LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Red, 1.5f, true); g.DrawString(Code, font, brush, xLeft, 2); } else if (iValue == 1) { Font font = new System.Drawing.Font("楷体", 13, (FontStyle.Bold)); Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height); LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.DarkRed, 1.3f, true); g.DrawString(Code, font, brush, xLeft, 2); } else if (iValue == 2) { Font font = new System.Drawing.Font("宋体", 13, (System.Drawing.FontStyle.Bold)); Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height); LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Green, Color.Blue, 1.2f, true); g.DrawString(Code, font, brush, xLeft, 2); } else if (iValue == 3) { Font font = new System.Drawing.Font("黑体", 13, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Bold)); Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height); LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Green, 1.8f, true); g.DrawString(Code, font, brush, xLeft, 2); } } //////画图片的前景噪音点 //for (int i = 0; i < 8; 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); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } } }}
生成的验证码被保存在session中,判断就直接获取到用户输入的验证码比较Session[“CheckCode”]里的值就ok了
阅读全文
0 0
- asp.net生成图片验证码(数字)
- asp.net如何生成图片验证码
- asp.net生成验证码图片
- asp.net生成验证码图片
- asp.net生成验证码图片
- asp.net如何生成图片验证码
- asp.net生成图片验证码
- Asp.Net生成图片验证码(vb)
- asp.net如何生成图片验证码
- ASP.NET 生成图片验证码
- ASP.NET随机生成验证码图片
- asp.net 生成图片验证码
- ASP.NET 生成图片验证码
- 用ASP生成XBM数字图片(可用来生成验证码)
- ASP生成数字相加求和的BMP图片验证码
- ASP生成数字相加求和的BMP图片验证码
- ASP生成图形(数字验证码)
- asp.net 生成图形验证码(字母和数字混合)
- Qt 之QString
- PHP内核探索:PHP的FastCGI
- Androidstudio常见错误"Manifest merger failed with multiple errors, see logs"
- 网页浮动的理解及解决的办法
- POJ 3199 Uncle Jack 笔记
- asp.net生成图片验证码(数字)
- Material Design
- Linux下安装/启动/退出/命令行使用Memcached
- 网络请求库(ion、volley、retrofit)和图片加载库(glide、picasso、fresco)
- "-"控件通知到父窗口层处理.
- windows无法连接到group policy client服务.此问题阻止标准用户登陆系统.
- STUN和TURN技术浅析
- JSP内置对象学习案例:在线考试系统
- PHP内核探索:如何执行PHP脚本