Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response.WriteFile -- Write out a byte stream

Tags:

c#

asp.net

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?

like image 448
George Johnston Avatar asked Sep 21 '10 18:09

George Johnston


1 Answers

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);
like image 138
Oded Avatar answered Oct 03 '22 07:10

Oded