SoFunction
Updated on 2025-03-07

c# .net code to generate image verification code


<%@ Page Language="C#" AutoEventWireup="True" %>  
<%@ import Namespace=""%>  
<%@ import Namespace=".Drawing2D"%>  
<%@ import Namespace=""%>  
<script runat="server">  
 private void Page_Load(object sender,  e)  
 {  
  String sRndStr=(4);  
  (sRndStr);  
 }   
/*-----------------------------------------------------------------------------------*\  
 *  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=" c# .net checkcode class";  
 public const String auVersion="v1.0";  
 public const String au="";  
 public const String auEmail="@";  
 public const String auBlog="/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=;  
  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=(oImg))  
   {  
    HatchBrush hBrush = new HatchBrush(,  
      , );   
    (hBrush, 0, 0, ccWidth, ccWidth);  
    (sCc,new (ccFtFm,ccFtSz, ),  
     new (),0,0);   
//-----------------------------------------------------------------------------------------------------------------------------
    Pen blackPen = new Pen(, 1);  
(blackPen, 0, ccHeight, 0, 0); // Left vertical line
(blackPen, 0,0,ccWidth,0); // Top horizontal line
(blackPen, ccWidth-1,0, ccWidth-1,20); // Right vertical line
(blackPen, 0, ccHeight-1, ccWidth, ccHeight-1); // Base horizontal line
    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=(',');  
  Random oRnd=new Random();  
  Int32 iArLen=;  
  for(Int32 i=0; i<len; i++)  
  {  
   sTemp+=aRnd[(0,iArLen)];  
  }  
  return sTemp;  
 } // end public static String rndStr  
 //-----------------------------------end public static method  
 //-----------------------------------begin private static method  
 private static void writeImg(Bitmap oImg)  
 {  
  using( ms=new ())  
  {  
   (ms,);   
   ();    
   ="image/Png";  
   (());  
  }  
 } // end private static void writeImg  
}  
//---------------------------------------------------------------------end class checkcode  
</script>