zoukankan      html  css  js  c++  java
  • 验证码控件

        
            自定义控件部分    
                    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    using System.IO;
    using System.Drawing.Imaging;
    namespace CheckImageLib
    {
        [DefaultProperty(
    "Text")]
        [ToolboxData(
    "<{0}:CheckImages runat=server></{0}:CheckImages>")]
        
    public class CheckImages : WebControl
        
    {
            [Bindable(
    true)]
            [Category(
    "Appearance")]
            [DefaultValue(
    "")]
            [Localizable(
    true)]
            
    public string Text
            
    {
                
    get
                
    {
                    String s 
    = (String)ViewState["Text"];
                    
    return ((s == null? String.Empty : s);
                }


                
    set
                
    {
                    ViewState[
    "Text"= value;
                }

            }


         
            
    protected override void Render(HtmlTextWriter output)
            
    {

                output.Write(
    this.CreateCheckCodeImage(GenerateCheckCode()));

                
    //output.Write("");
            }

            
    private string GenerateCheckCode()
            
    {
                
    int number;
                
    char code;
                
    string checkCode = String.Empty;

                System.Random random 
    = new Random();

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

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

                    checkCode 
    += code.ToString();
                }

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

                
    return checkCode;
            }


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

                System.Drawing.Bitmap bm 
    = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
                Graphics bp 
    = Graphics.FromImage(bm);
                
                    
    //生成随机生成器
                    Random random = new Random();

                    
    //清空图片背景色
                    bp.Clear(Color.White);

                    
    //画图片的背景噪音线
                    for (int i = 0; i < 25; i++)
                    
    {
                        
    int x1 = random.Next(bm.Width);
                        
    int x2 = random.Next(bm.Width);
                        
    int y1 = random.Next(bm.Height);
                        
    int y2 = random.Next(bm.Height);

                        bp.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(00, bm.Width, bm.Height), Color.Blue, Color.DarkRed, 1.2ftrue);
                    bp.DrawString(checkCode, font, brush, 
    22);

                    
    //画图片的前景噪音点
                    for (int i = 0; i < 100; i++)
                    
    {
                        
    int x = random.Next(bm.Width);
                        
    int y = random.Next(bm.Height);

                       bm.SetPixel(x, y, Color.FromArgb(random.Next()));
                    }


                    
    //画图片的边框线
                    bp.DrawRectangle(new Pen(Color.Silver), 00, bm.Width - 1, bm.Height - 1);

                 
                   


                      FileStream fs 
    = new FileStream(Page.Server.MapPath(Page.Request.Url.AbsolutePath.Replace(".aspx"".jpg")), FileMode.Create);
                      bm.Save(fs, ImageFormat.Jpeg);

                      bm.Dispose();
                      bp.Dispose();
                      fs.Close();

                      
    return "<img src=" + Page.Request.Url.AbsolutePath.Replace(".aspx"".jpg"+ " ></img>";

                    
                }

               



          

        }

    }


    调用部分

     <form id="form1" runat="server">
        
    <div>
            
    &nbsp;<asp:TextBox ID="txtCheckCode" runat="server"></asp:TextBox>
            
    <asp:Label ID="lblMessage" runat="server"></asp:Label></div>
            
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />&nbsp;
           
            
            
    <table>
            
    <tr>
            
    <td style=" 7px">  
       
                
    &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<cc1:CheckImages ID="CheckImages1" runat="server" />
                
    &nbsp;
                
    &nbsp; &nbsp;
                
    &nbsp;
                
    &nbsp;&nbsp;</td>
            
    </tr>
            
    </table>
        
    </form>


      protected void Button1_Click(object sender, EventArgs e)
            
    {
                
    if (Request.Cookies["CheckCode"== null)
                
    {
                    lblMessage.Text 
    = "您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
                    lblMessage.Visible 
    = true;
                    
    return;
                }


                
    if (String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text, true!= 0)
                
    {
                    lblMessage.Text 
    = "验证码错误,请输入正确的验证码。";
                    lblMessage.Visible 
    = true;
                    
    return;
                }


            }
  • 相关阅读:
    Bootstrap表单验证插件bootstrapValidator使用方法整理
    bootStrap树形目录组件
    根据对象的某一属性进行排序的js代码(如:name,age)
    Ajax跨域:jsonp还是CORS
    用border-image实现波浪边框
    Ajax加载菊花loding效果
    写一个js向左滑动删除 交互特效的插件——Html5 touchmove
    js抛物线动画——加入购物车动效
    zepto弹出层组件
    原生JS实现购物车结算功能代码+zepto版
  • 原文地址:https://www.cnblogs.com/gwazy/p/908198.html
Copyright © 2011-2022 走看看