Library tutorials & articles

ASP.NET and GDI+

In the beginning

So you want to know how to generate an image with an ASP page on the fly? Well there is a few ways to do it, such as flash etc, but with Microsoft and GDI+ the task is made a lot easier. This tutorial just covers the basics, but from it you can always move on and experiment with GDI+ (and intellisense) :)

Well, lets start with the calling page default.aspx (or whatever you would like to call it):

I usually like to draw all my controls to a table using code, there are other ways to do it, but I like doing it this way:

  1. Add a TextBox to the default.aspx page calling it whatever you like (for the tutorial we'll call it txtWord)
  2. Add a CommandButton to the page and call it whatever you like (just to submit the form)
  3. Add a server side Table control (call it what you want I'll just call it imageTable)
  4. In the codebehind page (.aspx.vb) add the following code, or similar to the Page_Load procedure

         Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           ' #Check if the page is posted
           If Page.IsPostBack Then
               Dim m_sWord As String
               m_sWord = Request("txtWord")
               ' #Check if txtWord has a value
               If Not m_sWord = "" Then
                   GenImage(m_sWord)
               Else
                   Dim tRow As New TableRow
                   Dim tCell As New TableCell
                   tCell.Controls.Add(New LiteralControl("Error getting the word you posted"))
                   tRow.Controls.Add(tCell)
                   imageTable.Controls.Add(tCell)
               End If
           End If
         End Sub

  5. Add a function called GenImage:

         Public Function GenImage(ByVal sWord As String)
             '#Create the row and cell to hold the image in
             Dim tRow As New TableRow
             Dim tCell As New TableCell
             tCell.Controls.Add(New LiteralControl("<img src='img_gen.aspx?w=' & sWord & '>"))
             tRow.Controls.Add(tCell)
             imageTable.Controls.Add(tRow)
         End Function

So now you have a page that accepts a word posted to it by itself, and calls a aspx page as an image source. Now that was easy, next the fun part ... drawing :).

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of ASP.NET and GDI+.

Leave a comment

Sign in or Join us (it's free).

Ruan Meyer erm, what can I say... not much, 'cept that I like music, nwn, dnd, programming (C, C++, C#, VB) etc. and Jack Daniels... ;) oh yes and girls...

Related podcasts

  • Microsoft MSDN Videos: New Web Developer features in VS 2010

    Published 16 days ago, running time 0h0m

    Mike Ormond, from Microsoft's Developer and Platform Group, gives a session on the top 10 items that developers might be interested in learning more about in Visual Studio 2010 and ASP.net. .net, asp.net, vs2010, visual studio

Related jobs

Events coming up

  • Aug 25

    Top 10 Ways MVC 2 Will Boost Your Productivity

    Washington, United States

    Top 10 Ways MVC 2 Will Boost Your ProductivityMVC 1 was a huge release for ASP.NET developers looking to build applications with a cleaner architectural model and greater testability. With the release of MVC 2, the framework is maturing and enabling better productivity with more maintainable code.

Want to stay in touch with what's going on? Follow us on twitter or Facebook!