当前位置: 首页 > news >正文

验证码图片生成代码

ExpandedBlockStart.gif 验证码图片生成代码
     using  System.Drawing;

    
public   partial   class  Pub_validcode : System.Web.UI.Page
    {
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
this .CreateCheckCodeImage(GenerateCheckCode());
    }

    
// 生成随机数
     private   string  GenerateCheckCode()
    {
        
int  number;
        
char  code;
        
string  checkCode  =  String.Empty;

        System.Random random 
=   new  Random();

        
for  ( int  i  =   0 ; i  <   4 ; i ++ )
        {
            number 
=  random.Next();

            
if  (number  %   2   ==   0 )
                code 
=  ( char )( ' 0 '   +  ( char )(number  %   10 ));
            
else
                code 
=  ( char )( ' 0 '   +  ( char )(number  %   10 ));
            
// code = (char)('a' + (char)(number % 26));

            checkCode 
+=  code.ToString();
        }

        Response.Cookies.Add(
new  HttpCookie( " CheckCode " , checkCode));

        
// 将生成的随机数保存到session中

        Session[
" Num " =  checkCode.ToString().ToLower();

        
return  checkCode;
    }

    
private   void  CreateCheckCodeImage( string  checkCode)
    {
        
if  (checkCode  ==   null   ||  checkCode.Trim()  ==  String.Empty)
            
return ;

        System.Drawing.Bitmap image 
=   new  System.Drawing.Bitmap(( int )Math.Ceiling((checkCode.Length  *   12.5 )),  22 );
        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  System.Drawing.Font( " Arial " 12 , (System.Drawing.FontStyle.Bold  |  System.Drawing.FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush 
=   new  System.Drawing.Drawing2D.LinearGradientBrush( new  Rectangle( 0 0 , image.Width, image.Height), Color.Blue, Color.DarkRed,  1.2f true );
            g.DrawString(checkCode, font, brush, 
2 2 );

            
// 画图片的前景噪音点 
             for  ( int  i  =   0 ; i  <   100 ; 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  -   2 , image.Height  -   1 );

            System.IO.MemoryStream ms 
=   new  System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType 
=   " image/Gif " ;
            Response.BinaryWrite(ms.ToArray());
        }
        
finally
        {
            g.Dispose();
            image.Dispose();
        }
    }
}
 
ExpandedBlockStart.gif 前台调用代码
function  refresh()
{    
    $(
" imgValid " ).src = " ValidCode.aspx?temp= " +  ( new  Date().getTime().toString( 36 )); 
    
return   false ;
}
< img id = " imgValid "  src = " ValidCode.aspx "  onclick = " javascript:refresh(); "  alt  = ""/ >


转载于:https://www.cnblogs.com/hwx0807/archive/2010/08/09/1795670.html

相关文章:

  • 第十二章 管理存储过程
  • 数据之美(十一):30 套 Infographics 作品欣赏
  • jbpm4.3整合web工程时异常解决方案
  • JQERY limittext 插件0.2版
  • 家族企业传承问题研究
  • [2010-8-30]
  • jquery js 下载|jquery-1.4.2 下载|jquery最新版本下载
  • CYQ.Data 轻量数据层之路 使用篇三曲 MAction 取值赋值(十四)
  • 图释WSPBuilder的使用
  • 第K大数问题
  • GirdView实现折叠式效果
  • SQL语句优化方法30例(转)
  • 小技巧:批量另存
  • c# 保留2位小数 整数格式化的操作!
  • GridView导出Excel研究
  • -------------------- 第二讲-------- 第一节------在此给出链表的基本操作
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • Apache Pulsar 2.1 重磅发布
  • ES6核心特性
  • github从入门到放弃(1)
  • java 多线程基础, 我觉得还是有必要看看的
  • javascript数组去重/查找/插入/删除
  • Java方法详解
  • oldjun 检测网站的经验
  • react-native 安卓真机环境搭建
  • text-decoration与color属性
  • Vue2.x学习三:事件处理生命周期钩子
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • vue脚手架vue-cli
  • vue数据传递--我有特殊的实现技巧
  • 前端
  • 前端_面试
  • 前端相关框架总和
  • 前端之Sass/Scss实战笔记
  • 如何设计一个比特币钱包服务
  • 微信小程序开发问题汇总
  • 我这样减少了26.5M Java内存!
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • 第二十章:异步和文件I/O.(二十三)
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • #162 (Div. 2)
  • #HarmonyOS:基础语法
  • #宝哥教你#查看jquery绑定的事件函数
  • #我与Java虚拟机的故事#连载14:挑战高薪面试必看
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • (4)STL算法之比较
  • (9)STL算法之逆转旋转
  • (附源码)springboot人体健康检测微信小程序 毕业设计 012142
  • (三维重建学习)已有位姿放入colmap和3D Gaussian Splatting训练
  • (四)【Jmeter】 JMeter的界面布局与组件概述
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .NET CORE使用Redis分布式锁续命(续期)问题