i’m working on an ASP.net 2.0 c# project where i am creating a basic Captcha script. the html looks like this:
<img height="30" width="80" alt="" src="Captcha.aspx" />
and here is the code behind for 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;
}
this works fine on my local machine, but when uploaded to our dev server it fails. i don’t have direct access to the dev server to debug due to my role in the project so it’s a bit of trial and error atm.
any ideas?
turns out the server didn’t like the aspx page for some reason. so i moved the captcha to a ashx file and then it worked!