• web验证码


    前台引用.aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="YanZhengCode.aspx.cs" Inherits="YanZhengCode" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    <img id="Image1" onclick="javascript:suiji();"src="YanZhengCode.aspx" alt="看不清楚,请单击我。" border="0"style="height:25px;60px;border: 0px; cursor: pointer;" />
    <script language="javascript" type="text/javascript">
    function suiji() {
    document.getElementById("Image1").src = "gif_zl.aspx?chen=" + number;
    }
    </script>

    </div>
    </form>
    </body>
    </html>

     YanZhengCode.cs

    C#代码

    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Drawing;
    using System.IO;
    using System;

    public partial class YanZhengCode : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    //string str = Request["chen"];
    BuildCode();

    //ShuImg();
    }

    private void BuildCode()
    {

    Color[] strcolor = { Color.Red, Color.Blue, Color.Brown, Color.Black, Color.Tomato };

    string[] strfont = { "华文中宋" };
    Random rands = new Random();
    Bitmap images = new Bitmap(80, 26); //图片大小
    Graphics g = Graphics.FromImage(images);
    Font f = new Font(strfont[0], 20); //字体大小

    //笔刷填充色
    Brush b = new SolidBrush(Color.Tomato);
    Brush b2 = new SolidBrush(strcolor[rands.Next(0, strcolor.Length)]);
    Brush b3 = new SolidBrush(Color.Red);
    Brush b4 = new SolidBrush(Color.FromArgb(128, Color.Green));
    g.Clear(Color.Wheat);

    //线条颜色
    Pen p = new Pen(Color.Red, 0);
    Pen p2 = new Pen(Color.Turquoise, 0);
    Random rand = new Random();
    for (int i = 0; i < 40; i++)
    {
    int y = rand.Next(images.Height);
    int x = rand.Next(images.Width);
    //g.DrawLine(p, 0, y, images.Width, y);
    g.DrawRectangle(p2, x, y, 1, 1);
    }
    //int degree = 5;
    //Point tempPoint = new Point();
    //degree = ~degree + 1;
    //g.RotateTransform(degree);
    //tempPoint.X = 20;
    //tempPoint.Y = 4;

    g.DrawRectangle(p, 0, 0, images.Width - 1, images.Height - 1); //绘图区域
    string strCode = rand.Next(1000, 10000).ToString();
    Session["yanzhencode"] = strCode;
    g.DrawString(strCode.Substring(0, 1), f, b, 2, -1);
    g.DrawString(strCode.Substring(1, 1), f, b2, 19, -1);
    g.DrawString(strCode.Substring(2, 1), f, b3, 37, -1);
    g.DrawString(strCode.Substring(3, 1), f, b4, 50, -1);
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    images.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    Response.ClearContent();
    Response.ContentType = "image/jpeg";
    Response.BinaryWrite(ms.ToArray());
    //Response.Cookies.Add(new HttpCookie("chne", "6780"));
    g.Dispose();
    images.Dispose();
    }

    private void ShuImg()
    {
    Response.ContentType = "image/*"; //设置发送头为图像数据
    Response.Clear(); //清空发送数据
    Random rand = new Random();
    Stream stream = OpenFile(Server.MapPath("pic/" + rand.Next(1, 6).ToString() + ".jpg")); //打开文件流
    Bitmap bit = new Bitmap(stream);
    bit.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//保存到输出流中
    stream.Close();
    bit.Dispose();
    Response.Flush(); //输出到客户端
    }
    }

    **************************************二*******************

    using System;  using System.IO;  using System.Drawing;  using System.Drawing.Imaging;  using System.Text;  using System.Collections;  using System.Web;  using System.Web.UI;  using System.Web.UI.WebControls;  using System.Web.UI.WebControls.WebParts;  using System.Web.UI.HtmlControls;  /**//// <summary>  ///   /// ** asp.net(C#) 生成验证码 **  ///   ///  File: GenerateCheckCode.aspx.cs  ///   ///  Author: 周振兴 (Zxjay 飘遥)  ///   ///  E-Mail: tda7264@163.com  ///   ///  Date: 07-04-10  ///   /// </summary>  public partial class GenerateCheckCode : System.Web.UI.Page  ...{      protected void Page_Load(object sender, EventArgs e)      ...{  string chkCode = string.Empty;  //颜色列表,用于验证码、噪线、噪点  Color[] color =...{ Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };  //字体列表,用于验证码  string[] font =...{ "Times New Roman", "MS Mincho", "Book Antiqua", "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)];  }  Bitmap bmp = new Bitmap(100, 40);  Graphics g = Graphics.FromImage(bmp);  g.Clear(Color.White);  //画噪线  for (int i = 0; i < 10; i++)  ...{      int x1 = rnd.Next(100);      int y1 = rnd.Next(40);      int x2 = rnd.Next(100);      int y2 = rnd.Next(40);      Color clr = color[rnd.Next(color.Length)];      g.DrawLine(new Pen(clr), x1, y1, x2, y2);  }  //画验证码字符串  for (int i = 0; i < chkCode.Length; i++)  ...{      string fnt = font[rnd.Next(font.Length)];      Font ft = new Font(fnt, 18);      Color clr = color[rnd.Next(color.Length)];      g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)8);  }  //画噪点  for (int i = 0; i < 100; i++)  ...{      int x = rnd.Next(bmp.Width);      int y = rnd.Next(bmp.Height);      Color clr = color[rnd.Next(color.Length)];      bmp.SetPixel(x, y, clr);  }  //清除该页输出缓存,设置该页无缓存  Response.Buffer = true;  Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);  Response.Expires = 0;  Response.CacheControl = "no-cache";  Response.AppendHeader("Pragma", "No-Cache");  //将验证码图片写入内存流,并将其以 "image/Png" 格式输出  MemoryStream ms = new MemoryStream();  try  ...{      bmp.Save(ms, ImageFormat.Png);      Response.ClearContent();      Response.ContentType = "image/Png";      Response.BinaryWrite(ms.ToArray());  }  finally  ...{      //显式释放资源      bmp.Dispose();      g.Dispose();  }      }  }

    使用方法如下: 
    新建名为 GenerateCheckCode.aspx 的文件,将上述代码拷贝到代码文件 GenerateCheckCode.aspx.cs 
    在需要验证码的地方放置语句 <asp:Image ID="img1" runat="server" ImageUrl="~/GenerateCheckCode.aspx" /> 即可。

  • 相关阅读:
    【codecombat】 试玩全攻略 第二章 边远地区的森林 一步错
    【codecombat】 试玩全攻略 第十八关 最后的kithman族
    【codecombat】 试玩全攻略 第二章 边远地区的森林 woodlang cubbies
    【codecombat】 试玩全攻略 第二章 边远地区的森林 羊肠小道
    【codecombat】 试玩全攻略 第十七关 混乱的梦境
    【codecombat】 试玩全攻略 第二章 边远地区的森林 林中的死亡回避
    【codecombat】 试玩全攻略 特别关:kithguard斗殴
    【codecombat】 试玩全攻略 第二章 边远地区的森林 森林保卫战
    【codecombat】 试玩全攻略 第二章 边远地区的森林
    实验3 类和对象||
  • 原文地址:https://www.cnblogs.com/981971554nb/p/4733586.html
Copyright © 2020-2023  润新知