Cannot render image to HttpContext.Response.OutputStream

后端 未结 4 1702
面向向阳花
面向向阳花 2021-01-05 06:43

Basically I am trying to render a simple image in an ASP.NET handler:

public void ProcessRequest (HttpContext context)
{
    Bitmap image = new Bitmap(16, 16         


        
4条回答
  •  礼貌的吻别
    2021-01-05 07:10

    The writer indeed needs to seek to write in the stream properly.

    But in your last source code, make sure that you do use either MemoryStream.ToArray() to get the proper data or, if you do not want to copy the data, use MemoryStream.GetBuffer() with MemoryStream.Length and not the length of the returned array.

    GetBuffer will return the internal buffer used by the MemoryStream, and its length generally greater than the length of the data that has been written to the stream.

    This will avoid you to send garbage at the end of the stream, and not mess up some strict image decoder that would not tolerate trailing garbage. (And transfer less data...)

提交回复
热议问题