Rendering Graphics in ASP.NET with GDI+
Showing the Dynamic Image
To render the image, you can create a pagethe example calls ClockPage.aspxand drag and drop the ClockControl onto the ClockPage (see Figure 2).
When the ClockPage.aspx page is requested, the ClockControls Page_Load calls DrawClock. To ensure the ClockPage is requested, you assign it instead of an actual image to the source property of an <img> control (keeping in mind that an Image web control is rendered as an <img> HTML tag). Listing 3 shows the markup for the ClockPage.
Listing 3: The Markup for the clockpage.sspx Page
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ClockPage.aspx.vb" Inherits="ClockPage" %> <%@ Register Src="ClockControl.ascx" TagName="ClockControl" TagPrefix="uc1" %> <%@ OutputCache NoStore="true" Duration="1" VaryByParam="none" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</>title> </head> <body> <form id="form1" runat="server"> <div> <uc1:ClockControl ID="ClockControl1" runat="server" EnableViewState="false" /> </div> </form> </body> </html>
I'll leave the final challenge of animating the clock as real-time feedback for another article. As is, the user would have to refresh the page every time an update clock was desired.
The Clock Is Drawn
This example demonstrated how you can treat a dynamic image on a UserControl as a drawing surface much as you'd treat a Windows form as one. The user control is then embedded on a page, and that page becomes the source value of an image attribute. Each time the source attributeI used the <img src="clockpage.aspx" /> tagrequested and the image is to be rendered, the clock will be redrawn.
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his new book UML DeMystified from McGraw-Hill/Osborne. Paul is a software architect for Tri-State Hospital Supply Corporation. You may contact him for technology questions at email@example.com.
If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.
Copyright © 2007 by Paul T. Kimmel. All Rights Reserved.
Page 2 of 2