用一般处理程序写验证码

验证码

创建一个一般处理程序:Code.ashx

不带字体旋转


public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string chkCode = "";
        //颜色列表,用于验证码、噪线、噪点
        Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue};
        //字体列表
        string[] font = {"Times New Roman","MS Mincho","Book Antigua","Gungsuh","PMingLIU","Impact" };
        char[] character = {'2','3','4','5','6','8','9','A','B'
               ,'C','D','E','F','G','H','J','K','L','M','N','P','R','S','T','W','X','Y' };
        
        Random rnd = new Random();
        //生成验证码字符串
        for (int i = 0; i < 4; i++)
        {
            chkCode+=character[rnd.Next(character.Length)];
        }
        //存储验证码操作。。。此处省略
        using (Bitmap img = new Bitmap(100, 30))
        {
            using (Graphics g=Graphics.FromImage(img))
            {
                try
                {
                    g.Clear(Color.White);
                    //画噪线
                    for (int i = 0; i < 10; i++)
                    {
                        int x1 = rnd.Next(100);
                        int y1 = rnd.Next(30);
                        int x2 = rnd.Next(100);
                        int y2 = rnd.Next(30);
                        Color clr = color[rnd.Next(color.Length)];
                        g.DrawLine(new Pen(clr), x1, y1, x2, y2);
                    }
                    //噪点
                    for (int i = 0; i < 100; i++)
                    {
                        int x = rnd.Next(img.Width);
                        int y = rnd.Next(img.Height);
                        Color clr = color[rnd.Next(color.Length)];
                        img.SetPixel(x, y, clr);
                    }
                   //把验证码画上去
                    for (int i = 0; i < chkCode.Length; i++)
                    {
                        string fnt = font[rnd.Next(font.Length)];
                        Font ft = new Font(fnt, 16);
                        Color clr1 = color[rnd.Next(color.Length)];
                        Color clr2 = color[rnd.Next(color.Length)];
                        System.Drawing.Drawing2D.LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), clr1, clr2, 1.2f, true);
                        g.DrawString(chkCode[i].ToString(), ft, brush, (float)i * 20 + 10, (float)(rnd.Next(2, 7)));
                        //new SolidBrush(clr)
                    }
                    //保存到写入内存流
                    MemoryStream stream = new MemoryStream();
                    img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    context.Response.Clear();
                    context.Response.ContentType = "image/jpeg";
                    context.Response.BinaryWrite(stream.ToArray());
                }
                catch (Exception)
                {
                }
                finally 
                {
                    g.Dispose();
                    img.Dispose();
                }
            }
        }
    }

点击图片,刷新图片(验证码)

      <script type="text/javascript">
          window.onload = function () {
              document.getElementById('imgCode').onclick = function () {
                  this.src = 'Code.ashx?id=' + Math.random();
              };
          };
    </script>
</head>
<body>
    <img id="imgCode" src="Code.ashx" />
</body>
================================================

验证码

创建一个一般处理程序:Code.ashx

有字体旋转


 public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string chkCode = "";
        //颜色列表,用于验证码、噪线、噪点
        Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue};
        //字体列表
        string[] font = {"Times New Roman","MS Mincho","Book Antigua","Gungsuh","PMingLIU","Impact" };
        char[] character = {'2','3','4','5','6','8','9','A','B'
               ,'C','D','E','F','G','H','J','K','L','M','N','P','R','S','T','W','X','Y' };
        
        Random rnd = new Random();
        //生成验证码字符串
        for (int i = 0; i < 4; i++)
        {
            chkCode+=character[rnd.Next(character.Length)];
        }
        //存储验证码操作。。。此处省略
        using (Bitmap img = new Bitmap(180, 40))
        {
            using (Graphics g=Graphics.FromImage(img))
            {
                try
                {
                    g.Clear(Color.WhiteSmoke);
                    //画噪线
                    for (int i = 0; i < 5; i++)
                    {
                        int x1 = rnd.Next(170);
                        int y1 = rnd.Next(50);
                        int x2 = rnd.Next(170);
                        int y2 = rnd.Next(50);
                        Color clr = color[rnd.Next(color.Length)];
                        g.DrawLine(new Pen(clr), x1, y1, x2, y2);
                    }
                    //噪点
                    for (int i = 0; i < 150; i++)
                    {
                        int x = rnd.Next(img.Width);
                        int y = rnd.Next(img.Height);
                        Color clr = color[rnd.Next(color.Length)];
                        img.SetPixel(x, y, clr);
                    }
                    //角度
                    float angle = 50;
                    float I = 0;
                   //把验证码画上去
                    for (int i = 0; i < chkCode.Length; i++)
                    {
                        Color clr1 = color[rnd.Next(color.Length)];
                        Color clr2 = color[rnd.Next(color.Length)];
                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        //关联插入模式为默认值
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        
                        string fnt = font[rnd.Next(font.Length)];
                        Font ft = new Font(fnt, 16);
                        
                        string s=new string(chkCode[i], 1);
                        g.ResetTransform();
                        SizeF size=g.MeasureString(s,ft);
                        size.Width = 35;
                        size.Height = 20;
                        //设置旋转中心为文字中心
                        g.TranslateTransform(I+size.Width/2,size.Height/2);
                        //旋转
                        g.RotateTransform((float)(rnd.NextDouble()*angle*2-angle));
                        //设置原点
                        g.TranslateTransform(20, -10);
                        System.Drawing.Drawing2D.LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0,0, img.Width, img.Height), clr1, clr2, 1.2f, true);
                        g.DrawString(s, ft, brush, new PointF((-size.Width /2), size.Height / 2));
                        I += size.Width;
                    }
                    //保存到写入内存流
                    MemoryStream stream = new MemoryStream();
                    img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    context.Response.Clear();
                    context.Response.ContentType = "image/jpeg";
                    context.Response.BinaryWrite(stream.ToArray());
                }
                catch (Exception)
                {
                }
                finally 
                {
                    g.Dispose();
                    img.Dispose();
                }
            }
        }
    }
点击图片,刷新图片(验证码)

     <script type="text/javascript">
          window.onload = function () {
              document.getElementById('imgCode').onclick = function () {
                  this.src = 'Code.ashx?id=' + Math.random();
              };
          };
    </script>
</head>
<body>
    <img id="imgCode" src="Code.ashx" />
</body>






阅读更多
个人分类: HTML 验证码 ashx
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭