我正在开发一个 ASP.net 2.0 c# 项目,我正在创建一个基本的验证码脚本。html 看起来像这样:
<img height="30" width="80" alt="" src="Captcha.aspx" />
这是 Captcha.aspx 背后的代码
protected void Page_Load(object sender, EventArgs e)
{
Bitmap objBMP = new System.Drawing.Bitmap(60, 20);
Graphics objGraphics = System.Drawing.Graphics.FromImage(objBMP);
objGraphics.Clear( ColorTranslator.FromHtml( "#054196" ) );
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
// configure the text
Font objFont = new Font("Arial", 8, FontStyle.Bold);
string randomStr = "";
int[] myIntArray = new int[5];
int x;
// randomise the text
Random autoRand = new Random();
for (x = 0; x < 5; x++)
{
myIntArray[x] = System.Convert.ToInt32(autoRand.Next(0, 9));
randomStr += (myIntArray[x].ToString());
}
//add string to session
Session.Add("randomStr", randomStr);
// draw the text
objGraphics.DrawString( randomStr, objFont, Brushes.White, 3, 3);
// Set the content type and return the image
Response.ContentType = "image/jpeg";
Encoder quality = Encoder.Quality;
EncoderParameter qualityParam = new EncoderParameter(quality, 100L);
EncoderParameters encParams = new EncoderParameters( 1 );
encParams.Param[0] = qualityParam;
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
objBMP.Save(Response.OutputStream, jpgEncoder, encParams);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();
Response.Flush();
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
这在我的本地机器上运行良好,但是当上传到我们的开发服务器时它失败了。由于我在项目中的角色,我无法直接访问开发服务器进行调试,所以这有点反复试验。
有任何想法吗?