public const string SESSION_VAR_NAME = "RandomStr";
protected void Page_Load(object sender, EventArgs e)
{
//http://msdotnetsupport.blogspot.com/2008/04/implement-simple-captcha-in-cnet.html 내용을 참고하였다.
//대조하는 방법도 해당 페이지를 참고한다.
//TODO: 좀더 다양한 색상, 글꼴 스타일 등등
Bitmap objBMP = new Bitmap(100, 30);
Graphics objGraphics = Graphics.FromImage(objBMP);
objGraphics.Clear(Color.Wheat);
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
//' Configure font to use for text
Font objFont = new Font("Arial", 14, FontStyle.Italic);
string randomStr = "";
char[] myArray = new char[5];
int x;
//That is to create the random # and add it to our string
Random autoRand = new Random();
for (x = 0; x < 5; x++)
{
myArray[x] = System.Convert.ToChar(autoRand.Next(65, 90));
randomStr += (myArray[x].ToString());
}
//This is to add the string to session, to be compared later
Session.Add(SESSION_VAR_NAME, randomStr);
//' Write out the Text
objGraphics.DrawString(randomStr, objFont, Brushes.Red, 3, 3);
//' Set the content type and return the image
Response.Buffer = false;
Response.Clear();
Response.ContentType = "image/GIF";
objBMP.Save(Response.OutputStream, ImageFormat.Gif);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();
Response.Flush();
Response.End();
}
후에 SessionRandomStr과 대조해보면 된다.