圖片驗證碼製作

來源:互聯網
上載者:User

標籤:end   efi   map   aws   random   doctype   art   cti   length   

default.aspx裡:包含點擊圖片更新的js代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title></head><body>    <form id="form1" runat="server">    <div>        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>        <asp:Image ID="Image1" runat="server" ImageUrl="YZM.aspx" />        <asp:Button ID="Button1" runat="server" Text="Button" />        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>    </div>    </form></body></html><script>//點擊圖片更新    var a = 0;    document.getElementById("Image1").onclick = function ()    {        this.setAttribute("src","yzm.aspx?id="+a);        a++;    }</script>

default.cs裡:驗證相同的按鈕事件寫在這裡

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class Default3 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        Button1.Click += Button1_Click;    }    void Button1_Click(object sender, EventArgs e)//驗證是否相同    {        string t1 = TextBox1.Text;        string t2 = Session["yzm"].ToString();        if (t1.ToUpper() == t2.ToUpper())        {            Label1.Text = "驗證成功";        }        else        { Label1.Text = "驗證失敗"; }    }}

YZM.aspx.cs裡:主要的繪製,產生寫在這裡

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Drawing;public partial class YZM : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        Bitmap img = new Bitmap(70, 30);        Graphics g = Graphics.FromImage(img);//繪製        Random r = new Random();        List<Color> clist = new List<Color>();//設定顏色集合        clist.Add(Color.Yellow);        clist.Add(Color.Black);        clist.Add(Color.Blue);        clist.Add(Color.Green);        clist.Add(Color.Pink);        clist.Add(Color.Orange);        for (int i = 0; i < 10; i++)        {            Color cl = clist[r.Next(0, clist.Count)];//隨機選取顏色            Pen pe = new Pen(new SolidBrush(cl),r.Next(1,5));//隨機產生顏色,隨機產生線的粗細            g.DrawLine(pe, new PointF(r.Next(0, 70), r.Next(0, 30)), new PointF(r.Next(0, 70), r.Next(0, 30)));//隨機產生線        }        string all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//設定驗證碼集合        string s = "";                for (int i = 0; i < 4; i++)//隨機產生四位驗證碼        {            s += all.Substring(r.Next(1,all.Length),1);        }        Session["yzm"] = s;//設定全域變數,用於驗證調用                       g.DrawString(s, new Font("微軟雅黑", 15), new SolidBrush(Color.Red), new PointF(5, 5));//繪製驗證碼        img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);//輸出資料流
      
     response.end(); }}

 

圖片驗證碼製作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.