zoukankan      html  css  js  c++  java
  • 一般处理程序 验证码

    一般处理程序 验证码
     
     
     
    public class 验é证¤码? : IHttpHandler
    {
    public void ProcessRequest(HttpContext context)
    {
    context.Response.ContentType = "image/JPEG";
    using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(200, 50))
    {
    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
    {
    g.DrawString("十ê?一ò?月?的μ?柳á?叶ò?",new System.Drawing.Font("宋?体ì?", 20), System.Drawing.Brushes.Green,new System.Drawing.PointF(0, 0));
    g.DrawEllipse(System.Drawing.Pens.Red, new System.Drawing.Rectangle(10, 10, 10, 10));
    g.DrawEllipse(System.Drawing.Pens.Red, new System.Drawing.Rectangle(20, 20, 10, 10));
    bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    }
    }
     
     
    随机验证码
     
    Random rand = new Random();
    int code = rand.Next();
    string strcode = Convert.ToString(code);
    g.DrawString(strcode, new System.Drawing.Font("宋?体ì?", 30), System.Drawing.Brushes.Green, new System.Drawing.PointF(0, 0));
    bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
     
     
    完整验证码示例:
     
    一般处理程序
    public void ProcessRequest(HttpContext context)
    {
    context.Response.ContentType = "image/JPEG";
    using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(200, 50))
    {
    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
    {
    //g.DrawString("十ê?一ò?月?的μ?柳á?叶ò?",new System.Drawing.Font("宋?体ì?", 20), System.Drawing.Brushes.Green,new System.Drawing.PointF(0, 0));
    //g.DrawEllipse(System.Drawing.Pens.Red, new System.Drawing.Rectangle(10, 10, 10, 10));
    //g.DrawEllipse(System.Drawing.Pens.Red, new System.Drawing.Rectangle(20, 20, 10, 10));
    //bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    Random rand = new Random();
    int code = rand.Next();
    string strcode = Convert.ToString(code);
    HttpContext.Current.Session["code"] = strcode;//session需Dè要òa通í¨过1yhttpcontext实êμ现?接ó口ú
    g.DrawString(strcode, new System.Drawing.Font("宋?体ì?", 30), System.Drawing.Brushes.Green, new System.Drawing.PointF(0, 0));
    bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    }
    前台
    <form id="form1" runat="server">
    <div>
    <input type="image" src="验é证¤码?.ashx" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" 
    onclick="Button1_Click" />
    </div>
    </form>
    后台
    protected void Button1_Click(object sender, EventArgs e)
    {
    string strcode = Session["code"].ToString();
    if (strcode==TextBox1.Text)
    {
    Response.Write("正y确è·");
    }
    else
    {
    Response.Write("错′í误ó");
    }
    }
    刷新验证码 <input type="image" src="验é证¤码?.ashx"  onclick="this.src='验证码.ashx?aaa='+new date()"/>




  • 相关阅读:
    接口测试小结
    UI自动化例子
    SQL Server 索引结构及其使用(二)
    SQL Server 索引结构及其使用(一)
    SQL Server 索引结构及其使用(四)
    SQL Server 索引结构及其使用(三)
    SQL Server 索引和视图
    Nginx 代理配置
    Java常见框架和工具
    MYSQL 存储过程 范例
  • 原文地址:https://www.cnblogs.com/liuzhiye/p/2705377.html
Copyright © 2011-2022 走看看