![]() |
Source code for makegraphic.aspx | Run the Sample | Home
<%@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.GIF) ' could be BMP, EMF, GIF, JPEG, PNG, TIFF, WMF ' Send stream to client Response.ClearContent() Response.ContentType = "image/gif" 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() %>
©2001 Alentus Corporation. All rights reserved. | Alentus Homepage | Alentus Support Site |