建立ValidateImg.aspx文件,里面代码如下:
<%@ Page Language="c#" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server" language="C#">
void Page_Load(Object sender, EventArgs e)
{
int charsNo = 4;
int fontSize = 12;
int bgWidth = 60;
int bgHeight = 20;
float x = (bgWidth - (charsNo * (fontSize + 0.5F))) / 2; // TODO: optimize
float y = (bgHeight - (fontSize * 1.7F)) / 2; // TODO: optimize
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Generate the text
//
//string genText = Globals.CreateTemporaryPassword(charsNo).ToUpper();
string genText = CreateTemporaryPassword(charsNo).ToUpper();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Add the generate text to a session variable
//
Session.Add("ValidateNumber", genText);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Create the memory map
//
Bitmap raster;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Select an memory image from file of 290x80 px
// in the current dir, NoSpamBgImgs folder named bg_X.jpg
//
Graphics graphicObj;
string bgFilePath = Server.MapPath(@".\AntiSpamBgImgs\bg_" + new Random().Next(1) + ".jpg");
System.Drawing.Image imgObj = System.Drawing.Image.FromFile(bgFilePath);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Creating the raster image & graphic objects
//
raster = new Bitmap(imgObj, bgWidth, bgHeight);
graphicObj = Graphics.FromImage(raster);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Creating an array for most readable yet cryptic fonts for OCR's
// This is entirely up to developer's discretion
// CAPTCHA recomandation
//
String[] crypticFonts = new String[13];
crypticFonts[0] = "Arial";
crypticFonts[1] = "Verdana";
crypticFonts[2] = "Comic Sans MS";
crypticFonts[3] = "Impact";
crypticFonts[4] = "Haettenschweiler";
crypticFonts[5] = "Lucida Sans Unicode";
crypticFonts[6] = "Garamond";
crypticFonts[7] = "Courier New";
crypticFonts[8] = "Book Antiqua";
crypticFonts[9] = "Arial Narrow";
crypticFonts[10] = "Fixedsys";
crypticFonts[11] = "宋体";
crypticFonts[12] = "仿体";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Instantiate object of brush with black color
//
Random r = new Random();
SolidBrush brushes = new SolidBrush(Color.FromArgb(r.Next(150), r.Next(150), r.Next(150)));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Loop to write the characters on image with different fonts
// CAPTCHA method
//
for (int a = 0; a < genText.Length; a++)
{
Font fontObj = new Font(crypticFonts[r.Next(crypticFonts.Length)], fontSize);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
graphicObj.DrawString(genText.Substring(a, 1), fontObj, brushes, x + (a * fontSize), y);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Flush again
//
graphicObj.Flush();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Setting the response header content MIME type
//
Response.ContentType = "image/gif";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Saving the file to output stream to be displayed in browser
//
raster.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Flushing to the Response
//
Response.Flush();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public static String CreateTemporaryPassword(int length)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string str = "";
Random r = new Random();
for (int i = 0; i < length; i++)
{
str += r.Next(0, 9).ToString();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return str;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
使用方法:
<img src="/ValidateImg.aspx" align="absmiddle" id="code" width="66" height="30" alt="验证码" /><a href="javascript:document.getElementById('code').src='/ValidateImg.aspx?'+Math.random();void(0);">看不清楚,换一个验证码</a>
后台:Session["ValidateNumber"] == input