这是我的代码:
HTML:<img src="thumbCreate.ashx?Id=223" alt="asd" />
HTTP 处理程序:`
public void ProcessRequest (HttpContext context)
{
CreateThumbNail(context);
}
private void CreateThumbNail(HttpContext context)
{
string resourceId = context.Request.QueryString["Id"];
context.Response.Write("No resource found for Id = " + resourceId);
Bitmap original = new Bitmap("C:/Devp/My work/ASHXSampleApp/Images/Desert.jpg");
int oWidth = original.Width;
int oHeight = original.Height;
int preferredWidth = 80;
int preferredHeight = 100;
int thumbWidthFactor = oWidth / preferredWidth;
int thumbHeightFactor = oHeight / preferredHeight;
int maxFactor = Math.Max(thumbWidthFactor, thumbHeightFactor);
int thumbNailWidth = oWidth / maxFactor;
int thumbNailHeight = oHeight / maxFactor;
Bitmap thumbNailImage = (Bitmap)original.GetThumbnailImage(thumbNailWidth, thumbNailHeight, ThumbNailCallback, IntPtr.Zero);
context.Response.ContentType = "image/Jpeg";
thumbNailImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}`
但是此代码不显示图像。当我手动尝试在 Firefox 中运行处理程序时,它给了我一个错误:-“无法显示图像“http://localhost:57157/ASHXSampleApp/thumbCreate.ashx?Id=223”,因为它包含错误。” 任何想法?