C# - Outputting image to response output stream giving GDI+ error

后端 未结 2 2073
时光说笑
时光说笑 2020-11-30 00:23

When outputting an image to the output stream, does it require temporary storage? I get the \"generic GDI+\" error that is usually associated with folder permission error wh

相关标签:
2条回答
  • 2020-11-30 01:02

    I just would add:

    Response.ContentType = "image/png";
    

    So it can be viewed directly in the browser when it isn't within an img tag.

    0 讨论(0)
  • 2020-11-30 01:06

    PNGs (and other formats) need to be saved to a seekable stream. Using an intermediate MemoryStream will do the trick:

    using (Bitmap image = new Bitmap(context.Server.MapPath("images/stars_5.png")))
    {
       using(MemoryStream ms = new MemoryStream())
       {
          image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
          ms.WriteTo(context.Response.OutputStream);
       }
    }
    
    0 讨论(0)
提交回复
热议问题