動態產生難圖片,參予使用者身分識別驗證

來源:互聯網
上載者:User

動態產生圖片頁面dyCreateImage.aspx

using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;

 protected void Page_Load(object sender, EventArgs e)
    {
        GenerateRandomImage(CheckCode());
    }
    private string CheckCode()
    {
        string CheckCode = string.Empty;
        Random rand = new Random();
        int temp;
        for (int i = 0; i < 4; i++)
        {
            temp = rand.Next(48,57);
            CheckCode += ((char)temp).ToString();
        }
        Response.Cookies.Add(new HttpCookie("CheckCode", CheckCode));
        return CheckCode;
    }
    private void GenerateRandomImage(string s)
    {
        if (s == null || s.Trim() == string.Empty)
            return;
        int IWidth = (int)Math.Ceiling(s.Length * 15m);
        int IHeight = 20;
        Bitmap img = new Bitmap(IWidth, IHeight);
        Graphics g = Graphics.FromImage(img);
        g.Clear(Color.White);
        try
        {
            Random rand = new Random();
            for (int i = 0; i < 20; i++)
            {
                int x1 = rand.Next(IWidth);
                int y1 = rand.Next(IHeight);
                int x2 = rand.Next(IWidth);
                int y2 = rand.Next(IHeight);
                g.DrawLine(new Pen(Brushes.Silver), x1, y1, x2, y2);
            }
            for (int i = 0; i < 50; i++)
            {
                int x1 = rand.Next(IWidth);
                int y1 = rand.Next(IHeight);
                img.SetPixel(x1, y1, Color.FromArgb(rand.Next()));
            }

            g.DrawRectangle(new Pen(Color.SaddleBrown), 0, 0, IWidth - 1, IHeight - 1);
            Font font = new Font("Arail", 14,  FontStyle.Bold);
            LinearGradientBrush lgb = new LinearGradientBrush(new Rectangle(0, 0, IWidth, IHeight), Color.Blue, Color.Purple, LinearGradientMode.Horizontal);
            g.DrawString(s, font, lgb, 1, 0);

            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, ImageFormat.Png);
                Response.Clear();
                Response.ContentType = "image/Png";
                Response.BinaryWrite(ms.ToArray());
                Response.End();
            }
        }
        finally
        {
            g.Dispose();
            img.Dispose();
        }
    }

 

調用頁面前台html代碼:

          <tr>
                                        <td align="middle" height="28">
                                            驗證碼:
                                        </td>
                                        <td width="27%">
                                            <input class="input_yz" id="_txtCode" runat="server" />
                                        </td>
                                        <td width="46%">
                                            <img id="imgRandom" src="dyCreateImage.aspx" onclick="javascript:changeImage();" width="50"
                                                height="19" alt="驗證碼" />
                                        </td>
                                    </tr>

        function changeImage()

{

   document.getElementById("imgRandom").src= document.getElementById("imgRandom").src+"?";

}

聯繫我們

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