E:\web\learnaspcom\htdocs\freebook\learn\ubtoc.xml LearnAsp.com - ASP ASP.net Free Lessons
Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
xxx

Make a Graphic on the Fly (PNG, TIFF, bmp, gif, ico)
by Christophe Wille & Brian Holland

These two pages demonstrates how to build a graphic file on the fly.  Christophe Wille supplied a VB and  C#  page (2 of the many languages an ASP.net page can be written in) so we can contrast these languages in a real situation.. It does show how C# is much closer to the ease of VB, and much further than the tedious and complex MS C/ATL/MFC.

Here is the VB.net sample by Christophe Wille:

   filename=/experiments/graphics/graphicmakevb.aspx

<Test Script Below>


<% @Page Language="VB" %>
<% @Import Namespace="System.Drawing" %>
<% @Import Namespace="System.IO" %>
<% @Import Namespace="System.Drawing.Imaging" %>
<%
Response.Expires = 0
Dim newBitmap as Bitmap '= null
Dim g as Graphics '= null
Dim sFont as string
Dim sFontSize as string
Dim sPhrase as string
Dim iFontSize as integer
Dim sColorBackground as string
Dim oColorBackground as color
Dim sFontColorName as string
dim ofont as color
Dim oFontColor as color
Dim oFontCounter as font
Dim oBitMap as bitmap
Dim oStream as MemoryStream
dim oStringSize as SizeF
Dim iWidth as integer
Dim iHeight as Integer
Dim oBitmapOut as bitmap

' Get the phrase to render
sPhrase = Request.QueryString.Get("Phrase")
'if sPhrase = Null then str2Render = "No Phrase specified"

' What font should we use?
sFont = Request.QueryString.Get("FontName")
'if sFont = Null Then sFont = "Lucida Sans Unicode"

' Which font size?
iFontSize = 12
sFontSize = Request.QueryString.Get("FontSize")
'if NOT (sFontSize = NULL) Then
    iFontSize = convert.toint32(sFontSize)
'End If

' How about a different background color
sColorBackground = Request.QueryString.Get("BackgroundColor")
oColorBackground = Color.White
'if NOT (sColorBackground = NULL) then
      oColorBackground = ColorTranslator.FromHTML(sColorBackground)
'end if

' Should we change the font color?
sFontColorName = Request.QueryString.Get("FontColor")
oFont = Color.Black
'if NOT (sFontColorName = NULL) Then
    oFont = ColorTranslator.FromHTML(sFontColorName)
'End If

' Get measurements of the image based on Font Size
oFontCounter = new Font(sFont, iFontSize)
oBitmap = new Bitmap(1,1,PixelFormat.Format32bppARGB)
g = Graphics.FromImage(oBitmap)
oStringSize = g.MeasureString(sPhrase, oFontCounter)
iWidth = cint(oStringSize.Width)
iHeight = cint(oStringSize.Height)
g.Dispose()
oBitmap.Dispose()

' Create a new image with background color, draw the phrase using font, fontcolor and font size
oBitmapOut = new Bitmap(iWidth,iHeight,PixelFormat.Format32bppARGB)
g = Graphics.FromImage(oBitmapOut)
g.FillRectangle(new SolidBrush(oColorBackground), new Rectangle(0,0,iWidth,iHeight))
g.DrawString(sPhrase, oFontCounter, new SolidBrush(oFont), 0, 0)

' Output the image as a PNG into a stream
oStream = new MemoryStream()
oBitmapOut.Save(oStream,ImageFormat.PNG)
' could be BMP, EMF, GIF, JPEG, PNG, TIFF, WMF

' Send stream to client
Response.ClearContent()
Response.ContentType = "image/PNG"
Response.BinaryWrite(oStream.ToArray())
Response.End

' Clean up
oFontCounter.Dispose()
oBitmapOut.Dispose()
g.Dispose()
'oStream.Dispose() ' Cant dispose of this -- get an error
'oStringSize.Dispose() ' Cant dispose of this -- get an error
'oFont.Dispose()

%>

Here is the C# Beta1 sample by Christophe Wille:

   filename=/test/pingmakecsbeta1.aspx

<Test Script Below>


Utility Belt ScriptColorCode cannot find the file:
E:\web\learnaspcom\htdocs\test\pingmakecsbeta1.aspx
There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.