• 验证码控件


        
            自定义控件部分    
                    
    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;
                }


            }
  • 相关阅读:
    什么是“泛在电力物联网”?要建一个什么样的泛在电力物联网?
    基于混合云雾计算的物联网架构
    探索 | “中医+AI”会诊电力设备故障
    泛在电力物联网有项核心技术 你听过没有?
    国网做泛在电力物联网的初衷是什么?如何参与?
    泛在电力物联网技术及战略解读:一个战略 两个领域 三个阶段
    构建“泛在电力物联网”成为国网当前最紧迫、最重要的任务
    如何解决分布式日志exceptionless的写入瓶颈
    SQL 查找是否"存在",别再 count 了,很耗费时间的
    abp vnext 微服务-基于Exceptionless实现分布式日志记录
  • 原文地址:https://www.cnblogs.com/gwazy/p/908198.html
Copyright © 2020-2023  润新知