c# .net 產生圖片驗證碼的代碼

來源:互聯網
上載者:User

說明:
.net 萬歲...
.net framework 的類庫真是太強了, 用 GDI+ 可以幹N多N多事情.
廣告時間:
shawl.qiu C# CMS 系統 預計40天后開始編碼, 現在逐步設計中, 免得到時求職說什麼什麼作品...唉.
PS: 今天求職真是慘不忍睹, 誰要招網頁相關的請聯絡 13435580019, 邱先生.
什麼地方俺都去, 工資只要能過活就行, 但是食宿問題得解決.
shawl.qiu
2007-02-01
http://blog.csdn.net/btbtd
class checkcode: 複製代碼 代碼如下:<%@ 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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.