Sending Dynamically Created Images to the Browser

In the previous section, we examined how to create and save a dynamic image to the Web server's file system through an ASP.NET page. Although this capability is quite useful, at times it would be nice to bypass this step and stream the dynamically created image directly to the Web visitor's browser.

Fortunately, this is possible with the Save method of the Image class. In Listing 2.4.1, we looked at one use of the Save method, passing it a file path on the Web server and an ImageFormat. The Save method can also accept a Stream object as its first parameter; in doing so, the Save method will send the output of the image to the stream as opposed to disk.

With ASP.NET, the Response object has a number of new properties and methods. One such property is the OutputStream property, which provides programmatic access to the binary data being sent to the client's browser. The Save method of the Image class can use the Response object's OutputStream property to send a dynamically created, in-memory image directly to the client! Listing 2.18 contains a short code example in which a standard advertising banner is created on-the-fly and squirted to the user's browser.

JPEG Specifies the JPEG image format.

MemoryBMP Specifies the memory bitmap image format.

PhotoCD Specifies the Eastman-Kodak PhotoCD image format.

PNG Specifies the PNG image format.

TIFF Specifies the TIFF image format.

WMF Specifies the Windows metafile image format. 2

function, we clean up the resources used by calling the Dispose method for the Graphics class | p

Listing 2.18 Dynamically Created Images Can Be Sent Directly to the User's Browser

<%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Imaging" %> <script language="VB" runat="server"> Sub Page_Load(source as Object, e as EventArgs) Dim objBitmap as Bitmap = new Bitmap(468, 60) Dim objGraphics as Graphics = Graphics.Fromlmage(objBitmap) Dim objBrush as SolidBrush = new SolidBrush(Color.FromARGB(0,80,80)), _ objBlackBrush as SolidBrush = new SolidBrush(Color.Black), _ objYellowBrush as SolidBrush = new SolidBrush(Color.Yellow)

Dim fontTitle as Font = new Font("Arial", 24), _

fontSubtitle as Font = new Font("Arial Black", 9)

objGraphics.FillRectangle(new SolidBrush(Color.Ivory), 0, 0, 468, 60) objGraphics.DrawString("When you think ASP, think...", _

fontSubtitle, objBlackBrush, 5, 8) objGraphics.DrawString("4GuysFromRolla.com", fontTitle, objBrush, 10, 20)

1 Draw a smiley face., first draw the yellow circle! objGraphics.FillEllipse(objYellowBrush, 375, 5, 50, 50)

1 Now create the eyes.

objGraphics.FillEllipse(objBlackBrush, 387, 20, 6, 6) objGraphics.FillEllipse(objBlackBrush, 407, 20, 6, 6)

1 And finally the smile. Dim aPoints(4) as Point aPoints(0).X = 383 aPoints(1).X = 395 aPoints(2).X = 405 aPoints(3).X = 417

objGraphics.DrawCurve(new Pen(Color.Black), aPoints)

Response.ContentType = "image/jpeg" objBitmap.Save(Response.OutputStream, objGraphics.Dispose() objBitmap.Dispose() End Sub </script>

Listing 2.18 begins as Listing 2.17 did: by Importing the System.Drawing and System.Drawing.Imaging namespaces. Next, in the Page_Load event handler, a 468x60

Bitmap instance is created (the dimensions of the standard advertising banner; on line 5). On line 6, a Graphics instance is created and instantiated with the Graphics.Fromlmage method. On lines 7 through 9, a number of SolidBrushes are created and instantiated; lines 11 and 12 create the two Fonts we'll be using.

From lines 14 through 32, a number of Graphics methods are called to create the image. Finally, after the image is created, it's time to send it to the browser. Before we send the actual binary content to the browser, however, we set the binary data's ContentType as "image/jpeg" (line 34). This ContentType informs the browser on how to display the binary information that it is receiving.

To actually send the binary data of the image, we use the Save method. Note that, on line 35, instead of specifying a filename as the first parameter, we specify the Stream to write the image to. Because we want to send the image to the browser, we specify that the OutputStream of the Response object be used. Also, the image should be converted and sent as a JPEG. Listing 2.18 concludes with a little house cleaning on lines 37 and 38.

One way to view the dynamically created image in Listing 2.18 is to visit the ASP.NET page that creates it and squirts it out to the OutputStream of the Response object. Figure 2.16 depicts a browser visiting the ASP.NET page directly.

tn m

Figure 2.16

The dynamically created image can be viewed by directly visiting the ASP.NET page.

Figure 2.16

The dynamically created image can be viewed by directly visiting the ASP.NET page.

Ideally, it would be nice to be able to display both content and the dynamically generated graphic on the same page. This, however, cannot be done through the same ASP.NET page; for example, if you were to add Response.Write("Hello, World!") in line 33 of Listing 2.18, instead of seeing an image when visiting the ASP.NET page, you'd see a broken image link. Essentially, this is because the browser is being sent "Hello, World!" and then the binary data for the image, and it assumes that the "Hello, World" is part of the binary data for the image.

If you want to display both textual content and a dynamically created image, you must create a standard HTML or ASP.NET page and then refer to the dynamic image content using HTML IMG tags. Listing 2.19 contains a very short example of an ASP.NET page that displays HTML content and the dynamic image created in Listing 2.18. Listing2.l8.aspx, the ASP.NET page that creates the image, is referred to in the IMG tag as if it were a JPG image itself. Figure 2.17 contains a screenshot of Listing 2.19 when viewed through a browser.

0 0

Post a comment

  • Receive news updates via email from this site