Creating dynamic images in ASP.NET

You might know that cre­at­ing images in ASP was not pos­si­ble with­out some sort of third party com­po­nent or some plug-in to the browser or server. Many web­sites bought Macro­me­dia Gen­er­a­tor which could together with ASP cre­ate nice images on the server and then send them to the browser. But now with .NET and the new ver­sion of ASP.NET it has become embarass­ingly sim­ple for everyone.

You might know that cre­at­ing images in ASP was not pos­si­ble with­out some sort of third party com­po­nent or some plug-in to the browser or server. Many web­sites bought Macro­me­dia Gen­er­a­tor which could together with ASP cre­ate nice images on the server and then send them to the browser. But now with Microsoft.NET and the new ver­sion of ASP ASP.NET it has become embarass­ingly sim­ple for every­one (if you only have ASP, then I've included links to some web­sites that make server-side objects for you to use).

In ASP.NET the name­spaces sec­tion is cru­cial. With the right name­spaces we are able to call and use prop­er­ties and func­tions that is nec­es­sary for this
code to work. Please view com­ments in the code.


<%@ Page Language="VB" ContentType="image/jpeg" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing.Drawing2D" %>
<%
Response.Clear( )

'Set height and width of the image
Dim intheight As Integer = 400
Dim intwidth As Integer = 150

Dim bmpimage As new Bitmap(intwidth, intheight,
PixelFormat.Format24bppRgb)
Dim objimage as Graphics = Graphics.FromImage(bmpimage)

'Set alias and color to fill with
objimage.SmoothingMode = SmoothingMode.AntiAlias
objimage.Clear(Color.LightBlue)

'Set properties of the lines...
objimage.DrawRectangle(Pens.White,1,1,intwidth-3,intheight-3)
objimage.DrawRectangle(Pens.Black,1,1,intwidth,intheight)

 'Set text; what text, font and where to write it (two axis).
 objimage.DrawString("Kick-Ass!!!", _
New Font("Arial", 12, FontStyle.italic), _
SystemBrushes.WindowText, New PointF(30,50))
objimage.DrawString("Search and see", _
New Font("Arial", 12, FontStyle.italic), _
SystemBrushes.WindowText, New PointF(25,75))
objimage.DrawString("All .NET", _
New Font("Arial", 12, FontStyle.italic), _
SystemBrushes.WindowText, New PointF(35,150))
objimage.DrawString("www.ngcode.com", _
New Font("Arial", 8, FontStyle.Bold), _
SystemBrushes.WindowText, New PointF(20,200))

 'Display the image as a jpg
 bmpIMAGE.Save(Response.OutputStream, ImageFormat.Jpeg)
objimage.Dispose( )
bmpIMAGE.Dispose( )

Response.End( )

%>

Other rec­om­mended sites if you have ASP.NET

Other rec­om­mended sites if you only have ASP:

3 comments
  1. joe says: Jan 17, 200512:51 pm

    Your arti­cle are great and very infor­ma­tive. I read your arti­cle on
    embed­ding text in to an image.

    I like to know how to embed a small image (small 10 by 10 gif file)
    into a larger image thats retrieve from the web (embed the image into
    the lower right cor­ner) and then dis­play­ing it in a results HTML page.

    I know its prob­a­bly very sim­ple to you but I am scratch­ing my head on
    this. Please pro­vide some help on this.

    Your faith­full reader

  2. Neil says: Jun 14, 20058:34 am

    Hi, I have the pearl ver­sion of imagemag­ick on an asp.net server. We are look­ing to do some basic con­ver­sions with gif files which we know can be done eas­ily (make the gif trans­par­ent and change the color based on user input). We are not sure though, if a script will work on asp.net and if it can be done, how to do it. Can you help? Thanks!!

  3. Signs Of Change says: Nov 24, 20051:46 am

    Thanks for the post, I have been stuck using ASPim­age through my brinkster host­ing account till I read your arti­cle, nice to know I have other options ;)

Submit comment