Is is possible to write to the http response stream from a dynamically created bitmap using the Response.Write/WriteFile without saving the image to the hard drive?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use a MemoryStream and assign it to Response.OutputStream, or simply use Response.OutputStream directly when saving the bitmap.
There is an example in the documentation on this page, though it simply 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);
Method 2
If you have your bitmap stored in a byte[] you can also dump that directly into Response.BinaryWrite(myByteArray);, as long as you have your content-type, length and disposition set correctly (as mentioned by @arx).
Method 3
How about Response.BinaryWrite?
Method 4
Yes. Make sure you set the content-type correctly and it should work fine.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0