Response.WriteFile - 写出一个字节流

Geo*_*ton 7 c# asp.net

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

Ode*_*ded 11

您可以使用a 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);
Run Code Online (Sandbox Code Playgroud)