程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 【轉】C#生成驗證碼

【轉】C#生成驗證碼

編輯:C#入門知識

 

使用C#生成驗證碼圖片,然後在網頁中直接使用,很方便的用法。

System;
System.Drawing;
System.Drawing.Imaging;
System.Web.UI;
System.Drawing.Drawing2D;
System.IO;

{
    ValidateCode()
    {
    }
    MaxLength
    {
        { 10; }
    }
    MinLength
    {
        { 1; }
    }
    CreateValidateCode(length)
    {
        [] randMembers = [length];
        [] validateNums = [length];
        validateNumberStr = ;
        seekSeek = (().Now.Ticks);
        seekRand = (seekSeek);
        beginSeek = ()seekRand.Next(0, .MaxValue - length * 10000);
        [] seeks = [length];
        (i = 0; i < length; i++)
        {
            beginSeek += 10000;
            seeks[i] = beginSeek;
        }
        (i = 0; i < length; i++)
        {
            rand = (seeks[i]);
            pownum = 1 * ().Pow(10, length);
            randMembers[i] = rand.Next(pownum, .MaxValue);
        }
        (i = 0; i < length; i++)
        {
            numStr = randMembers[i].ToString();
            numLength = numStr.Length;
            rand = ();
            numPosition = rand.Next(0, numLength - 1);
            validateNums[i] = .Parse(numStr.Substring(numPosition, 1));
        }
        (i = 0; i < length; i++)
        {
            validateNumberStr += validateNums[i].ToString();
        }
        validateNumberStr;
    }

    [] CreateValidateGraphic(validateCode)
    {
        Bitmap image = Bitmap(().Ceiling(validateCode.Length * 12.0), 24);
        Graphics g = Graphics.FromImage(image);
        {
            random = ();
            g.Clear(Color.White);
            (i = 0; i < 25; i++)
            {
                x1 = random.Next(image.Width);
                x2 = random.Next(image.Width);
                y1 = random.Next(image.Height);
                y2 = random.Next(image.Height);
                g.DrawLine(Pen(Color.Silver), x1, y1, x2, y2);
            }
            Font font = Font(, 12, (FontStyle.Bold | FontStyle.Italic));
            LinearGradientBrush brush = LinearGradientBrush(Rectangle(0, 0, image.Width, image.Height),
             Color.Blue, Color.DarkRed, 1.2f, );
            g.DrawString(validateCode, font, brush, 3, 2);
            (i = 0; i < 100; i++)
            {
                x = random.Next(image.Width);
                y = random.Next(image.Height);
                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }
            g.DrawRectangle(Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
            stream = ();
            image.Save(stream, ImageFormat.Jpeg);
            stream.ToArray();
        }
        {
            g.Dispose();
            image.Dispose();
        }
    }
    GetImageWidth(validateNumLength)
    {
        ()(validateNumLength * 12.0);
    }
    GetImageHeight()
    {
        22.5;
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved