developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Remote Online Backup
Laptops
Boat Donations
Memory
Shop Online
Find Software
Domain registration
Auto Insurance Quote
KVM Switch over IP
Online Education
Shop
Web Design
Disney World Tickets
Web Hosting Directory

 
Biz Resources
Network Security Services
VoIP
CRM Software


Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Project Management Guide: Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.

Rendering Graphics in ASP.NET with GDI+
By Paul Kimmel

Go to page: 1  2  Next  

As I write this, Vista is out and promises new and cool things, and Windows Presentation Foundation (WPF) is closing the gap between rich Windows clients and rich web clients. So, a huge number of developers and businesses out there are interested in rich web clients. For them, I wrote this article.

This article uses a trivial example to demonstrate how to dynamically create GDI+ graphics in web pages. The example, a ticking clock, has plumbing that would support any kind of continuously updating, dynamically rendered graphic.

Rendering Graphics with GDI+

Although web forms do not have a canvas (or device context, also called DC) and you therefore cannot ask a Web form for its Graphics object, you can simulate this behavior by rendering graphics in ASP.NET. In summary, to render images with GDI+ you need:

  • One user control that will act as your device context or drawing canvas
  • One web page to contain the user control
  • One web page to contain an image control (The first web page will actually play the role of the image for this web page.)

Figure 1 depicts the relationship between the user control and two web pages.



Click here for a larger image.

Figure 1: The Relationship Between the Controls that Play the Role of Dynamic Canvas

One page is actually the viewable page, and the second page and user control play the role of dynamic canvas. Take a look at how this works by building it from the inside out.

Defining the UserControl

The UserControl is as close to the Graphics object (canvas or device context, if you prefer) as you are going to get. The basic idea of rendering the image is to create an image, get a Graphics object from that image, draw something on it, and then save that image on the HttpResponse.OutputStream (this code is show in Listing 1).

Listing 1: The Code That Orchestrates Rendering the Clock

Private Sub DrawClock()
   Dim b As Bitmap   = New Bitmap(FClockWidth, FClockHeight)
   Dim g As Graphics = Graphics.FromImage(b)
   Dim p As Pen      = New Pen(Brushes.Black, 1)
   g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
   DrawClockFace(g, p)
   DrawClockTicks(g, p)
   DrawClockHands(g, p)
   Response.ContentType = "image/jpeg"
   b.Save(Response.OutputStream, ImageFormat.Jpeg)
   Response.End()
End Sub

The reason I used an inner UserControl is that writing to the output stream is destructive to things already on the stream. So, other HTML would be obliterated by writing the image. When you save the image to the output response stream, that is all that will be there. The code that renders the clock is not that important technically, but Listing 2 shows it for fun.

Listing 2: The ClockControl (The ClockControl.ascx Page Is an Empty UserControl.)

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Partial Class ClockControl
  Inherits System.Web.UI.UserControl
  Private FClockWidth As Integer = 100
  Private FClockHeight As Integer = 100
  Private Sub DrawClock()
    Dim b As Bitmap   = New Bitmap(FClockWidth, FClockHeight)
    Dim g As Graphics = Graphics.FromImage(b)
    Dim p As Pen      = New Pen(Brushes.Black, 1)
    g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    DrawClockFace(g, p)
    DrawClockTicks(g, p)
    DrawClockHands(g, p)
    Response.ContentType = "image/jpeg"
    b.Save(Response.OutputStream, ImageFormat.Jpeg)
    Response.End()
  End Sub
  Private Sub DrawClockFace(ByVal g As Graphics, ByVal p As Pen)
    g.FillRectangle(Brushes.White, 0, 0, FClockWidth, FClockHeight)
    g.DrawEllipse(p, New Rectangle(1, 1, 98, 98))
  End Sub
  Private Sub DrawClockTicks(ByVal g As Graphics, ByVal p As Pen)
    ' draw the clock ticks
    ' Borrowed from Mike Gold's article: 
    ' http://www.vbdotnetheaven.com/UploadFile
    '/mgold/VirtualClockinVBdotNET04212005032826AM/
    'VirtualClockinVBdotNET.aspx
    Dim count As Integer = 1
    Dim hour As Integer = 1
    Dim angle As Double
    For count = 0 To 330 Step 30
      angle = (count - 1) * Math.PI / 180
      'For angle = 0 To (2 * Math.PI) - (2.0 * Math.PI / 14)
      'Step 2.0 * Math.PI / 12
      Dim x As Double = (FClockWidth - 20) / 2 *_
         Math.Cos((angle - Math.PI / 3)) + _
        (FClockWidth - 20) / 2 + 5
      Dim y As Double = (FClockWidth - 20) / 2 * _
         Math.Sin((angle - Math.PI / 3)) + _
        (FClockWidth - 20) / 2 + 4
      Dim font As Font = New Font("Times New Roman", 8)
      g.DrawString(Convert.ToString(hour), font, Brushes.Black, _
         CSng(x), CSng(y), _
        New StringFormat)
      'count += 1
      hour += 1
    Next count ' angle
  End Sub
  Private Sub DrawClockHands(ByVal g As Graphics, ByVal p As Pen)
    Dim d As DateTime = DateTime.Now
    ' draw hour hand
    Dim h As Integer = d.Hour
    DrawHour(g, h)
    ' draw minute hand
    Dim m As Integer = d.Minute
    DrawMinute(g, m)
    ' draw second hand
    Dim s As Integer = d.Second
    DrawSecond(g, s)
  End Sub
  Private Sub DrawHour(ByVal g As Graphics, ByVal hour As Integer)
    Const OFFSET As Integer = 30
    Dim p As Pen = New Pen(Color.Black, 3)
    ' Figure out the Angle in radians
    Dim angle As Double = ((hour - 1) Mod 12) * 30 * Math.PI / 180
    Dim x, y As Double
    x = (FClockWidth - OFFSET) / 2 * _
        Math.Cos((angle - Math.PI / 3)) + _
      (FClockWidth - OFFSET) / 2 + 10
    y = (FClockWidth - OFFSET) / 2 * _
       Math.Sin((angle - Math.PI / 3)) + _
      (FClockWidth - OFFSET) / 2 + 8

    g.DrawLine(p, CSng(FClockWidth / 2), CSng(FClockHeight / 2), _
      CSng(x), CSng(y))
  End Sub
  Private Sub DrawMinute(ByVal g As Graphics, _
                         ByVal Minute As Integer)
    Dim p As Pen = New Pen(Color.Black, 2)
    ' Figure out the Angle in radians
    Dim angle As Double = (Minute - 6) * 6 * Math.PI / 180
    Dim x, y As Double
    x = (FClockWidth - 20) / 2 * Math.Cos((angle - Math.PI / 3)) + _
      (FClockWidth - 20) / 2 + 10
    y = (FClockWidth - 20) / 2 * Math.Sin((angle - Math.PI / 3)) + _
      (FClockWidth - 20) / 2 + 8
    g.DrawLine(p, CSng(FClockWidth / 2), CSng(FClockHeight / 2), _
       CSng(x), CSng(y))
  End Sub
  Private Sub DrawSecond(ByVal g As Graphics, _
                         ByVal Second As Integer)
    Dim p As Pen = New Pen(Color.Red, 1)
    ' Figure out the Angle in radians
    Dim angle As Double = (Second - 6) * 6 * Math.PI / 180
    Dim x, y As Double
    x = (FClockWidth - 20) / 2 * Math.Cos((angle - Math.PI / 3)) + _
      (FClockWidth - 20) / 2 + 10
    y = (FClockWidth - 20) / 2 * Math.Sin((angle - Math.PI / 3)) + _
      (FClockWidth - 20) / 2 + 8
    g.DrawLine(p, CSng(FClockWidth / 2), CSng(FClockHeight / 2), _
       CSng(x), CSng(y))
  End Sub

  Protected Sub Page_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Load
    DrawClock()
  End Sub
End Class

The last three lines of code in DrawClock move the dynamic image to the output stream. You will need to use System.Drawing and System.Drawing.Image to support the ClockControl behavior.

Go to page: 1  2  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


ASP & ASP.NET Archives

Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
Five Trends for Application Development. Download Your Complimentary Report. Exclusive. Act Now.
Intel Go Parallel Portal: Translating Multicore Power into Application Performance
Best Practices for Developing a Web Site. Checklists, Tips & Strategies. Download Exclusive eBook Now.
Whitepaper: Enterprise Information Integration--Deployment Best Practices for Low-Cost Implementation



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES