Click here to Skip to main content
12,759,601 members (32,988 online)
Click here to Skip to main content
Add your own
alternative version

Stats

98.4K views
1.4K downloads
40 bookmarked
Posted 28 Dec 2004

Create Dynamic Images in ASP.NET

, 29 Dec 2004
Rate this:
Please Sign up or sign in to vote.
This article is based on Manster's article and extension is given to his/her code in VB.NET version.

Sample image

Introduction/Inspiration

I was searching for similar stuff in the net for quite some time and found the article written by Manster. Before this article, I found few other articles too, but they didn't explain how to render image without saving it to the disk. After seeing this code and reading the posts in the feedback section by other members asking "How to show image in image control", I decided to get the thing done.

Testing the application

  • Quite easy and simple, just extract the files and create a "testing" web application mapping to the extracted directory.

    Or

  • Add the Default.* and ImageGenerator.* files to your existing web application. Just type the URL in the browser.

Using the code

As I said, this article is based on Manster's article, I am not going to explain much how the Dynamic Image Generation works. You may read base article here.

In my demo project(default.aspx) I have used four elements.

  • Image Control with default text set.
  • Image Control to demonstrate Dynamic Image Generation.
  • HTML Image element (<img>).
  • One more HTML Image element with font size set.

For the value of ImageUrl (Image control)/ src (HTML <img> element), just pass the text you want to convert to image as HTML query string. E.g.:

<img src="ImageGenerator.aspx?imgtext=Hello&fontsize=24">

This will generate image text "Hello" with font size 24. Same way works with the Image control, pass the ImageURL parameter same as above (or just check the demo code). If you don't pass the query string, then you will see a "?" (default text). Default font size is set to 10.

Here comes the core which I copy, pasted from the site and converted to VB.NET code.

'The following lines were added as Extention
Dim sImageText As String
Dim iFontSize As Integer
sImageText = Server.UrlDecode(Request.QueryString("imgtext"))
iFontSize = CInt(Server.UrlDecode(Request.QueryString("fontsize")))
'Just fool proofing, in case, "imgtext" parameter is not passed
If sImageText.Trim.Length = 0 Then sImageText = "?"
If iFontSize = 0 Then iFontSize = 12
'Rest of the code goes as is

Dim bmpImage As New Bitmap(1, 1)

Dim iWidth As Integer = 0
Dim iHeight As Integer = 0

'// Create the Font object for the image text drawing.
Dim MyFont As New Font("Verdana", iFontSize, _
           System.Drawing.FontStyle.Bold, _
           System.Drawing.GraphicsUnit.Point)

'// Create a graphics object to measure the text's width and height.
'Graphics(MyGraphics = Graphics.FromImage(bmpImage))
Dim MyGraphics As Graphics
MyGraphics = Graphics.FromImage(bmpImage)

'// This is where the bitmap size is determined.
iWidth = MyGraphics.MeasureString(sImageText, MyFont).Width
iHeight = MyGraphics.MeasureString(sImageText, MyFont).Height

'// Create the bmpImage again with the correct size for the text and font.
bmpImage = New Bitmap(bmpImage, New Size(iWidth, iHeight))

'// Add the colors to the new bitmap.
MyGraphics = Graphics.FromImage(bmpImage)
MyGraphics.Clear(Color.White)
MyGraphics.TextRenderingHint = TextRenderingHint.AntiAlias
MyGraphics.DrawString(sImageText, MyFont, New SolidBrush(Color.Brown), 0, 0)
MyGraphics.Flush()

'Return bmpImage
Response.ContentType = "image/gif" 'Tell browser, what we are sending
bmpImage.Save(Response.OutputStream, ImageFormat.Gif)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

dev2dev
Database Developer Valuelabs
India India
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralAnother similar article Pin
rajendra malav6-Jul-09 2:19
memberrajendra malav6-Jul-09 2:19 
GeneralGreat article Pin
Member 358742027-May-09 7:16
memberMember 358742027-May-09 7:16 
GeneralSimilar functionality but packaged nicely as a webservice: Pin
Member 87063027-May-09 4:52
memberMember 87063027-May-09 4:52 
GeneralAnother approach Pin
alex2320-Oct-08 2:57
memberalex2320-Oct-08 2:57 
GeneralSuggestions to imporve Pin
dev2dev29-Dec-04 19:19
memberdev2dev29-Dec-04 19:19 
GeneralRe: Suggestions to imporve Pin
drokliss20-Jul-06 9:45
memberdrokliss20-Jul-06 9:45 
GeneralRe: Suggestions to imporve Pin
FF7710-Apr-08 14:00
memberFF7710-Apr-08 14:00 
GeneralRe: Suggestions to imporve Pin
DanGetz26-Aug-08 12:55
memberDanGetz26-Aug-08 12:55 
GeneralRe: Suggestions to imporve Pin
tim waite21-May-10 3:41
membertim waite21-May-10 3:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 29 Dec 2004
Article Copyright 2004 by dev2dev
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid