I know, not a very advanced topic, but still one of those things you need every now and then. And when you need it, it’s a good thing that the solution is all over the Internet. And now it’s on my blog too .
Using a Handler for this would probably have been the more beautiful solution, but this one (with an aspx page) works fine too. Default.aspx only contains an asp:Image control. The Image control has it’s source set to Image.aspx. Image.aspx, in turn, draws a blue rectangle with a red square in it (pretty neat, huh?) :
private Bitmap drawImage()
//Create a bitmap
Bitmap bmp = new Bitmap(100, 100);
//Get the Graphics object from the bitmap (using it's Image base)
Graphics g = Graphics.FromImage((System.Drawing.Image)bmp);
//Color it blue
g.FillRectangle(new Pen(Color.Blue).Brush, 0, 0, 100, 100);
//Draw the red rectangle
g.DrawRectangle(new Pen(Color.Red), new Rectangle(5, 5, 90, 90));
//and we're done
Then, in the Page_Load event, the image is streamed to the browser:
protected void Page_Load(object sender, EventArgs e)
//Clear the responsestream, just to be sure.
//Set the Responsetype
Response.ContentType = "image/jpeg";
//Save the dynamicly generated bitmap to the OutpuStream
//End we're done.
It’s as simple as that.
Download Solution – ImageStreamer.zip