SniptoolsSniptools | Design & Technology Observations

RSS

Creating dynamic images in ASP.NET

Mar 9th 2003
3 Comments

Respond
Trackback

You might know that creating images in ASP was not possible without some sort of third party component or some plug-in to the browser or server. Many websites bought Macromedia Generator which could together with ASP create nice images on the server and then send them to the browser. But now with .NET and the new version of ASP.NET it has become embarassingly simple for everyone.

You might know that creating images in ASP was not possible without some sort of third party component or some plug-in to the browser or server. Many websites bought Macromedia Generator which could together with ASP create nice images on the server and then send them to the browser. But now with Microsoft.NET and the new version of ASP ASP.NET it has become embarassingly simple for everyone (if you only have ASP, then I’ve included links to some websites that make server-side objects for you to use).

In ASP.NET the namespaces section is crucial. With the right namespaces we are able to call and use properties and functions that is necessary for this
code to work. Please view comments 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 recommended sites if you have ASP.NET

Other recommended sites if you only have ASP:




This post is tagged

3 Comments

  1. joe

    Your article are great and very informative. I read your article on
    embedding 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 corner) and then displaying it in a results HTML page.

    I know its probably very simple to you but I am scratching my head on
    this. Please provide some help on this.

    Your faithfull reader

  2. Neil

    Hi, I have the pearl version of imagemagick on an asp.net server. We are looking to do some basic conversions with gif files which we know can be done easily (make the gif transparent 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

    Thanks for the post, I have been stuck using ASPimage through my brinkster hosting account till I read your article, nice to know I have other options ;)

Incoming Links