小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

生成驗證碼

 實力決定地位 2015-09-25
1首先建一個aspx頁面代碼如下
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web.AjaxPage.OtherRequest
{
    public partial class VialCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //設(shè)置不緩存此頁
            Response.AppendHeader("pragma", "no-cache");
            Response.AppendHeader("Cache-Control", "no-cache, must-revalidate");
            Response.AppendHeader("expires", "0");
            Random rand = new Random();
            //獲取隨機字符
            string str = GetRandString(4);
            Session["nowcodecode"] = str;//保存驗證碼
            //創(chuàng)建畫板
            Bitmap image = new Bitmap(63, 25);
            Graphics g = Graphics.FromImage(image);
            //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.InterpolationMode = InterpolationMode.Low;
            //g.CompositingMode = CompositingMode.SourceOver;
            //g.CompositingQuality = CompositingQuality.HighQuality;
            g.CompositingQuality = CompositingQuality.HighSpeed;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //繪制漸變背景
            Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
            Brush brushBack = new LinearGradientBrush(rect, Color.FromArgb(rand.Next(150, 256), 255, 255), Color.FromArgb(255, rand.Next(150, 256), 255), rand.Next(90));
            g.FillRectangle(brushBack, rect);
            //繪制干擾曲線
            for (int i = 0; i < 2; i++)
            {
                Point p1 = new Point(0, rand.Next(image.Height));
                Point p2 = new Point(rand.Next(image.Width), rand.Next(image.Height));
                Point p3 = new Point(rand.Next(image.Width), rand.Next(image.Height));
                Point p4 = new Point(image.Width, rand.Next(image.Height));
                Point[] p = { p1, p2, p3, p4 };
                Pen pen = new Pen(Color.Gray, 1);
                g.DrawBeziers(pen, p);
            }
            //逐個繪制文字
            for (int i = 0; i < str.Length; i++)
            {
                string strChar = str.Substring(i, 1);
                int deg = rand.Next(-15, 15);
                float x = (image.Width / str.Length / 2) + (image.Width / str.Length) * i;
                float y = image.Height / 2;
                //隨機字體大小
                Font font = new Font("Consolas", rand.Next(16, 24), System.Drawing.FontStyle.Regular);
                SizeF size = g.MeasureString(strChar, font);
                Matrix m = new Matrix();
                //旋轉(zhuǎn)
                m.RotateAt(deg, new PointF(x, y), MatrixOrder.Append);
                //扭曲
                m.Shear(rand.Next(-10, 10) * 0.03f, 0);
                g.Transform = m;
                //隨機漸變畫筆
                Brush brushPen = new LinearGradientBrush(rect, Color.FromArgb(rand.Next(0, 256), 0, 0), Color.FromArgb(0, 0, rand.Next(0, 256)), rand.Next(90));
                g.DrawString(str.Substring(i, 1), font, brushPen, new PointF(x - size.Width / 2, y - size.Height / 2));
                g.Transform = new Matrix();
            }
            g.Save();
            Response.ContentType = "image/jpeg";
            Response.Clear();
            Response.BufferOutput = true;
            image.Save(Response.OutputStream, ImageFormat.Jpeg);
            g.Dispose();
            image.Dispose();
            Response.Flush();
        }
        //獲取隨機數(shù)
        private string GetRandString(int len)
        {
            //string s = "0123456789";
            string s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string str = "";
            Random r = new Random();
            for (int i = 0; i < len; i++)
            {
                str += s.Substring(r.Next(s.Length), 1);
            }
            return str;
        }
    }
}
2顯示驗證碼圖片的地方
 <td id="td1"><span id="vcodetxt"></span>驗證碼:</td>
                <td>
                    <input type="text" class="input" id="txtcode" style="width:55px;"  />
                </td>
                <td>
                    <img alt="刷新驗證碼" src="AjaxPage/OtherRequest/VialCode.aspx" id="codeimage" onclick="this.src=this.src+'?'" />
                </td>

 
3怎么校驗驗證碼是否輸入正確
當(dāng)然產(chǎn)生的時候就存入session然后登陸的時候和session比較就OK啦

 
4注意:onclick="this.src=this.src+'?'"這個是為了點擊圖片的時候從新生成

 
5效果如下
 
 

 

 
 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約