0
我正在創建一個基本的驗證碼腳本的ASP.net 2.0 c#項目。在html看起來是這樣的:jpeg無法加載服務器上的Response.Outputstream
<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;
}
這個作品我的本地機器上很好,但是當上傳到我們的開發服務器失敗。由於我在項目中的角色,我沒有直接訪問dev服務器進行調試,所以這是一個試驗和錯誤atm。
有什麼想法?
是否引發任何異常? –
您至少可以使用Fiddler或FireBug來檢查請求和響應(有時它發生在生產環境中,該URL是錯誤的,並且請求不會簡單地到達它所想的位置)。 – tpeczek
螢火蟲在控制檯中吐出'圖像被破壞或被截斷'。 – rohanlatimer