首页 > 开发 > .Net > 正文

c# .net 生成图片验证码的代码

2023-04-26 12:25:45
字体:
来源:转载
供稿:网友

为了增强网站的安全性,我们在网站的登录模块或信息输入模块加入了验证码功能,那么在ASP.net中如何实现验证码呢?本文借助GDI+实现了这个功能。

.net中实现验证码功能的源代码如下:

<%@ Page Language="C#" AutoEventWireup="True" %>
  <%@ import Namespace="System.Drawing"%>
  <%@ import Namespace="System.Drawing.Drawing2D"%>
  <%@ import Namespace="System.Web"%>

  <script runat="server">
  private void Page_Load(object sender, System.EventArgs e)
  {
  String sRndStr=checkcode.rndStr(4);
  checkcode.general(sRndStr);
  }
  /*-----------------------------------------------------------------------------------*/
  * shawl.qiu c# .net checkcode class v1.0
  /*-----------------------------------------------------------------------------------*/
  //------------------------------begin class checkcode
  public class checkcode
  {
  //-----------------------------------begin event
  public checkcode()
  {
  }
  ~checkcode()
  {
  }
  //-----------------------------------end event

  //-----------------------------------begin public constant
  //-----------------------begin about
  public const String auSubject="shawl.qiu c# .net checkcode class";
  public const String auVersion="v1.0";
  public const String au="shawl.qiu";
  public const String auEmail="shawl.qiu@gmail.com";
  public const String auBlog="http://blog.csdn.net/btbtd";
  public const String auCreateDate="2007-2-1";
  //-----------------------end about
  //-----------------------------------end public constant

  //-----------------------------------begin public static method
  public static void general(String sCc)
  {
  Int32 ccLen=sCc.Length;
  String ccFtFm="Arial";
  Int32 ccFtSz=12;
  Int32 ccWidth=ccLen*ccFtSz+1;
  Int32 ccHeight=ccFtSz+5;
  using(Bitmap oImg = new Bitmap(ccWidth, ccHeight))
  {
  using(Graphics oGpc=Graphics.FromImage(oImg))
  {
  HatchBrush hBrush = new HatchBrush(HatchStyle.DashedVertical,
  Color.Yellow, Color.Silver);
  oGpc.FillRectangle(hBrush, 0, 0, ccWidth, ccWidth);
  oGpc.DrawString(sCc,new System.Drawing.Font(ccFtFm,ccFtSz, FontStyle.Bold),
  new System.Drawing.SolidBrush(Color.Black),0,0);
  //-----------------------边框
  Pen blackPen = new Pen(Color.Black, 1);
  oGpc.DrawLine(blackPen, 0, ccHeight, 0, 0); // 左竖线
  oGpc.DrawLine(blackPen, 0,0,ccWidth,0); // 顶横线
  oGpc.DrawLine(blackPen, ccWidth-1,0,ccWidth-1,20); // 右竖线
  oGpc.DrawLine(blackPen, 0, ccHeight-1, ccWidth, ccHeight-1); // 底横线
  writeImg(oImg);
  }
  }
  } // end public static void general

  public static String rndStr(Int32 len)
  {
  String sTemp="";
  String sForRnd="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
  String[] aRnd=sForRnd.Split(',');
  Random oRnd=new Random();
  Int32 iArLen=aRnd.Length;
  for(Int32 i=0; i<len; i++)
  {
  sTemp+=aRnd[oRnd.Next(0,iArLen)];
  }
  return sTemp;
  } // end public static String rndStr
  //-----------------------------------end public static method

  //-----------------------------------begin private static method
  private static void writeImg(Bitmap oImg)
  {
  using(System.IO.MemoryStream ms=new System.IO.MemoryStream())
  {
  oImg.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
  HttpContext.Current.Response.ClearContent();
  HttpContext.Current.Response.ContentType="image/Png";
  HttpContext.Current.Response.BinaryWrite(ms.ToArray());
  }
  } // end private static void writeImg

  }

  //---------------------------------------------------------------------end class checkcode
  </script>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表