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

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

页面

using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Drawing;  namespace AMPlatForm.Union.Online{    public partial class ValiCode : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            DoImg(DoCode(5));        }        ///         /// 生成验证码表达式        ///         /// 验证码的长度        /// 
验证码表达式
private string DoCode(int num) { //要从这个数组中随机取字符来生成验证码 string[] Codesource ={
"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"}; string code = ""; Random rd = new Random(); for (int i = 0; i < num; i++) { code += Codesource[rd.Next(0, Codesource.Length)]; } return code; } /// /// 生成图片 /// /// 验证码表达式 private void DoImg(string code) { Random random = new Random(); Bitmap mypalette = new Bitmap(100, 30); Graphics gh = Graphics.FromImage(mypalette); using (gh) { Rectangle rc = new Rectangle(0, 0, 100, 30); //填充图片 gh.FillRectangle(new SolidBrush(Color.White), rc); for (int i = 0; i < 1; i++) { int x1 = random.Next(mypalette.Width); int x2 = random.Next(mypalette.Width); int y1 = random.Next(mypalette.Height); int y2 = random.Next(mypalette.Height); //在图片上随机画线条 gh.DrawLine(new Pen(Color.Blue), x1, y1, x2, y2); } gh.DrawString(code, new Font("宋体", 20), new SolidBrush(Color.Black), rc); for (int i = 1; i < 60; i++) { int x = random.Next(mypalette.Width); int y = random.Next(mypalette.Height); //在图片中填充随机颜色 mypalette.SetPixel(x, y, Color.FromArgb(random.Next())); } //将生成的图像保存到流中 mypalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); Session["ValiCode"] = code; //主要用来验证 } Response.Write(code); } }}

 前端局部刷新

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="Test.WebForm3" %>        
验证码测试请输入验证码:
刷新验证码

注意加参数

图片自己刷新

    
验证码测试请输入验证码:

 

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

你可能感兴趣的文章
angular之$watch方法详解
查看>>
OpenCV中Mat的列向量归一化
查看>>
Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例【附详细代码】
查看>>
提升编程能力的11个技巧
查看>>
Windows Server 2008 各版本功能差异与比较各版本概观--转载
查看>>
一套最全的JavaScript 语言基础知识点总结(思维导图10张)
查看>>
【原】iOS学习之第三方-AFNetworking1.3.0
查看>>
元素的content属性
查看>>
popoverController使用注意
查看>>
关于Could not parse configuration: /hibernate.cfg.xml的问题
查看>>
python-第三方模块
查看>>
十天冲刺之六
查看>>
开启telnet
查看>>
个人冲刺第四天
查看>>
OpenStack开启sshd
查看>>
Java基础语法
查看>>
Zabbix4.0+第三方报警平台OneAlert监控报警
查看>>
C#中如何使用断点操作调试程序
查看>>
hdu1513 经典dp:添加最少字符使字符串回文:(滚动数组优化空间)
查看>>
YAML Class ID Reference
查看>>