C #. net code for generating image verification Codes

Source: Internet
Author: User

Note:
Long live. net...
The. net framework class library is really too strong. With GDI +, you can do more than N tasks.
Ad Time:
Shawl. qiu C # The CMS system is expected to start coding in 40 days. Now it is gradually being designed, so that it will not be necessary to ask for a job to do anything... alas.
PS: today, my job is terrible. If you want to recruit webpages, contact Mr. Qiu at 13435580019.
Wherever I go, I only need to pay for living, but the problem of accommodation and accommodation must be solved.
Shawl. qiu
2007-02-01
Http://blog.csdn.net/btbtd
Class checkcode:
Copy codeThe Code is as follows:
<% @ 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 );
}
/* Optional /*-----------------------------------------------------------------------------------*\
* Shawl. qiu c #. net checkcode class v1.0
\ * Optional \*-----------------------------------------------------------------------------------*/
// ------------------------------------------------------------------------- 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 );
// ----------------------- Border
Pen blackPen = new Pen (Color. Black, 1 );
OGpc. DrawLine (blackPen, 0, ccHeight, 0, 0); // left vertical line
OGpc. DrawLine (blackPen, 0, 0, ccWidth, 0); // Top horizontal line
OGpc. DrawLine (blackPen, ccWidth-1, 0, ccWidth-1, 20); // right vertical line
OGpc. DrawLine (blackPen, 0, ccHeight-1, ccWidth, ccHeight-1); // baseline
WriteImg (oImg );
}
}
} // End public static void general
Public static String rndStr (Int32 len)
{
String sTemp = "";
String sForRnd = ", 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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.