C# 返回图像的页面

C# 返回图像的页面,c#,asp.net,C#,Asp.net,我想让asp.net页面返回图像到浏览器这里是代码 protected void Page_PreRender(object sender, EventArgs e) { Response.Clear(); string iName = Request.QueryString.Get("ImageName") ; Bitmap bmp = new Bitmap(Server.MapPath("a.png")); Resp

我想让asp.net页面返回图像到浏览器这里是代码

  protected void Page_PreRender(object sender, EventArgs e)
    {
        Response.Clear();
        string iName = Request.QueryString.Get("ImageName") ;
        Bitmap bmp = new Bitmap(Server.MapPath("a.png"));
        Response.ContentType = "image/png";
        bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
        bmp.Dispose();
        Response.End();
    }
我离线工作很好,我的意思是在VisualStudio中运行时,但在godady托管环境中运行时,会出现此错误

A generic error occurred in GDI+.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[ExternalException (0x80004005): A generic error occurred in GDI+.]
   System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) +471002
   System.Drawing.Image.Save(Stream stream, ImageFormat format) +36
   PageReturnImageDeals.MyImage.Page_PreRender(Object sender, EventArgs e) +191
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnPreRender(EventArgs e) +92
   System.Web.UI.Control.PreRenderRecursiveInternal() +83
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18055 
1) 当你不使用它的时候,为什么要找回我

2) HttpHandler更适合这种操作

3) 不需要对文件执行任何图形操作-它只是简单的传输

Handler.ashx.cs:

public class Handler : IHttpHandler {
  public bool IsReusable {
    get { return false; }
  }

  public void ProcessRequest(HttpContext context) {
    var realPath = Server.MapPath("a.png");
    context.Response.ContentType = "image/png";
    context.Response.WriteFile(realPath);
  }
}
编辑:

如果您坚持将其作为第页:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Clear();
    var realPath = Server.MapPath("a.png");
    Response.ContentType = "image/png";
    Response.WriteFile(realPath);
}

您应该使用。或者使用二进制流来读取图像文件并写入响应,而不是使用位图。以下是您的答案:场景不同,但我在这个问题中做得很简单Ondreji尝试了这个解决方案,它确实有效,但如何通过.aspx页面而不是在HttpHandler中完成呢?这可能吗?原因是什么在测试环境中工作而不是在生产环境中工作?