zoukankan      html  css  js  c++  java
  • 验证码

    public class RandomHandler : IHttpHandler,IRequiresSessionState
    {
       
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "image/gif";
            Bitmap map = new Bitmap(110,23);
            Graphics g = Graphics.FromImage(map);
            g.Clear(Color.White);

            this.DrawPoint(ref g);
            this.DrawLine( ref g);
           
            Color codeColor=Color.FromArgb(RandomBuilder.Next(0, 255), RandomBuilder.Next(0, 255), RandomBuilder.Next(0, 255));
            string code = RandomBuilder.GetRandomCode(6);
            context.Session["Code"] = code;

            g.DrawString(code,new Font("Arial Black",14),new SolidBrush(codeColor),new PointF(0.0f,0.0f));
            g.Flush();
            g.Dispose();


            map.Save(context.Response.OutputStream,ImageFormat.Jpeg);
            context.Response.End();
            map.Dispose();
        }

        private void DrawPoint(ref Graphics g)
        {
            int pointCount = RandomBuilder.Next(30,70);
           
            for(int i =0;i<pointCount;i++)
            {
                g.DrawRectangle(new Pen(
                    new SolidBrush(Color.FromArgb(RandomBuilder.Next(0, 255), RandomBuilder.Next(0, 255), RandomBuilder.Next(0, 255)
                        ))
                    ), RandomBuilder.Next(0, 100), RandomBuilder.Next(0, 30)
                    ,1,1
                    );
                g.Flush();
                continue;
            }
          
        }

        private void DrawLine(ref Graphics g)
        {
            int lineCount = RandomBuilder.Next(1, 10);

            for (int i = 0; i < lineCount; i++)
            {
               
                int x1=RandomBuilder.Next(0,100);
                int x2=RandomBuilder.Next(0,100);
                int y1=RandomBuilder.Next(0,30);
                int y2=RandomBuilder.Next(0,30);
                g.DrawLines(new Pen(
                    new SolidBrush(Color.FromArgb(RandomBuilder.Next(0, 255), RandomBuilder.Next(0, 255), RandomBuilder.Next(0, 255)
                        ))
                    ),new Point[]{new Point(x1,y1),new Point(x2,y2)}
                    );
                g.Flush();
                continue;
            }
        }
     
        public bool IsReusable {
            get {
                return false;
            }
        }

    }

    调用方法:

    <img  id="img" src="RandomHandler.ashx" alt="验证码" onclick="this.src='RandomHandler.ashx?'+Math.random();" />

  • 相关阅读:
    文本框改造之多选下拉控件
    多附件上传控件
    Linq to Sql:更新之属性遍历法
    如何在HTML5页面中启动本地的App? 下面的方法应该可以。
    Nodejs 学习笔记-相片整理Demo(二)
    Nodejs 学习笔记-相片整理Demo(一)
    前端学习笔记一:什么是W3C?
    网页嵌入调用 全国各城市天气代码
    html页面清除缓存
    判断鼠标动作,可以给鼠标在标签不同区域的动作分别写不同的效果
  • 原文地址:https://www.cnblogs.com/weidehao555/p/2160249.html
Copyright © 2011-2022 走看看