C#產生圖形驗證碼

來源:互聯網
上載者:User

轉自 http://www.cnblogs.com/tuyile006/archive/2007/04/13/711874.html

產生驗證碼圖片的源碼:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;

public partial class CreatImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string checkCode = CreateCode(4);
        Session["CheckCode"] = checkCode;
        CreateImage(checkCode);
    }

    /*產生驗證碼*/
    public string CreateCode(int codeLength)
    {
       
        string so = "1,2,3,4,5,6,7,8,9,0,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,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[] strArr=so.Split(',');
        string code = "";
        Random rand=new Random();
        for (int i = 0; i < codeLength; i++)
        {
            code+=strArr[rand.Next(0, strArr.Length)];
        }
        return code;
    }

    /*產生驗證圖片*/
    public void CreateImage(string code)
    {
       
        Bitmap image = new Bitmap(60, 20);
        Graphics g = Graphics.FromImage(image);
        WebColorConverter ww=new WebColorConverter();
        g.Clear((Color)ww.ConvertFromString("#FAE264"));

        Random random = new Random();
        //畫圖片的背景雜音線
        for (int i = 0; i < 12; 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.LightGray), x1, y1, x2, y2);
        }
        Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Italic);
        System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
            new Rectangle(0,0,image.Width,image.Height),Color.Blue,Color.Gray,1.2f,true);
        g.DrawString(code, font, brush, 0, 0);

        //畫圖片的前景噪音點
        //for (int i = 0; i < 10; i++)
        //{
        //    int x = random.Next(image.Width);
        //    int y = random.Next(image.Height);
        //    image.SetPixel(x, y, Color.White);
        //}

        //畫圖片的邊框線
       // g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, 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());
        g.Dispose();
        image.Dispose();
    }
}

使用方法:
將以上代碼儲存在網頁:createImg.aspx中
在你的驗證碼圖片處:<img src="createImg.aspx" align="center">

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.