Response.WriteFile - write a stream of bytes

Can I write HTTP responses to a stream from a dynamically created bitmap using Response.Write / WriteFile without saving the image to my hard drive?

+7
source share
4 answers

You can use a MemoryStream and assign it Response.OutputStream or just use Response.OutputStream directly when saving a bitmap.

There is an example in the documentation on the page, although it just saves the bitmap directly to the output stream:

 // 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); 
+11
source share

If you have a bitmap stored in byte[] , you can also dump it directly in Response.BinaryWrite(myByteArray); if you have the content type, the length and location are set correctly (as @arx mentioned).

+3
source share
+2
source share

Yes. Make sure that you have configured the content type correctly and that it should work fine.

0
source share

All Articles