Response.WriteFile -- 输出字节流

7

是否可以在动态创建的位图中使用Response.Write/WriteFile向http响应流写入内容,而无需将图像保存到硬盘中?

4个回答

11
你可以使用一个 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);

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

2

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接