Response.WriteFile – 写出一个字节流

是否可以使用Response.Write / WriteFile从动态创建的位图写入http响应流而不将图像保存到硬盘驱动器?

您可以使用MemoryStream并将其分配给Response.OutputStream ,或者在保存位图时直接使用Response.OutputStream

此页面上的文档中有一个示例,但它只是将位图直接保存到输出流:

 // Set the correct content type, so browser/client knows what you are sending Response.ContentType = "image/jpeg"; Response.Clear(); Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmp); bmp.Save(Response.OutputStream, ImageFormat.Jpeg); 

如果你的位图存储在byte[]你也可以将它直接转储到Response.BinaryWrite(myByteArray); ,只要您正确设置了内容类型,长度和处置(如@arx所述)。

Response.BinaryWrite怎么样?

是。 确保正确设置内容类型,它应该可以正常工作。