dynamically creating a text image

Dhillon

New Member
The code below dynamically creates a text image. <BR>I modified the code from:<BR>http://asp101.com/articles/john/textimage/default.asp<BR><BR>The problem is that width of each character is different, which means the width of the image is hard to calculate. Is there a way to make an image just as big as needed? Right now it makes long words images too wide.<BR><BR><BR>Thank you<BR>Aaron<BR><BR><%@ Page Language="VB" Debug="True" Trace="True" %><BR><%@ Import Namespace="System.Drawing" %><BR><%@ Import Namespace="System.Drawing.Imaging" %><BR><%@ Import Namespace="System.Drawing.Text" %><BR><!--#INCLUDE Virtual="/includes/function_InstrCount.aspx"--><BR><%<BR>' Declare Vars<BR>Dim objBMP As System.Drawing.Bitmap<BR>Dim objGraphics As System.Drawing.Graphics<BR>Dim objFont As System.Drawing.Font<BR>Dim objColor As System.Drawing.Color<BR>Dim TextStr As String = Request.QueryString("TextStr") <BR>Dim FontName, myFontStyle As String<BR>Dim myFontSize As Integer <BR>Dim ImageHeight, ImageWidth As Integer<BR><BR>If Len(Request.QueryString("FontSize")) = 0 then<BR> myFontSize = 8<BR>else<BR> myFontSize = Request.QueryString("FontSize")<BR>End if<BR><BR>TextStr = Replace(TextStr, "<BR>", VBCRLF)<BR>If Len(TextStr) = 0 then<BR> TextStr = "line 1" & VBCRLF & "line 2" & VBCRLF & "line 3"<BR>End if<BR><BR><BR><BR><BR>FontName = Request.QueryString("FontName")<BR>If Len(FontName) = 0 then<BR> FontName = "Arial"<BR>End If <BR>Dim FontColor As String<BR>FontColor = Replace(Request.QueryString("FontColor"), "Hex:", "#")<BR>If Len(FontColor) = 0 then<BR> FontColor = "black"<BR>End If <BR><BR>Dim myBrush As SolidBrush = New SolidBrush(ColorTranslator.FromHtml(FontColor))<BR><BR><BR>' Configure font to use for text<BR>myFontStyle = Request.QueryString("FontStyle")<BR><BR>Select Case LCase(myFontStyle)<BR>Case "bold"<BR> objFont = New Font(FontName, myFontSize, FontStyle.Bold)<BR>Case "italic"<BR> objFont = New Font(FontName, myFontSize, FontStyle.Italic)<BR>Case "strikeout"<BR> objFont = New Font(FontName, myFontSize, FontStyle.Strikeout)<BR>Case "underline"<BR> objFont = New Font(FontName, myFontSize, FontStyle.Underline)<BR>Case Else<BR> objFont = New Font(FontName, myFontSize, FontStyle.Regular)<BR>End select<BR><BR><BR>' Create new image - bitmap<BR>' width, height <BR>'ImageHeight = (InStrCount(TextStr, VBCRLF) + 1) * (myFontSize*2)<BR>'ImageWidth = Len(TextStr)*((myFontSize-1)*2) <BR>ImageHeight = ((InStrCount(TextStr, VBCRLF) + 1) * objFont.Height) + 2 ' Counts how many line breaks to calculate the height <BR>ImageWidth = Len(TextStr)*((myFontSize-1)*2) ' Can't calculate this correctlly<BR>'ImageWidth = Len(TextStr)* objFont.<BR><BR><BR>objBMP = New Bitmap(ImageWidth, ImageHeight)<BR><BR><BR>'objBMP = New Bitmap(Len(TextStr)*16, 20)<BR><BR>' Create a graphics object to work with from the BMP<BR>objGraphics = System.Drawing.Graphics.FromImage(objBMP)<BR><BR>' Fill the image with background color<BR>objGraphics.Clear(Color.White)<BR><BR>' Set anti-aliasing for text to make it better looking<BR>objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias<BR><BR>objGraphics.DrawString(TextStr, objFont, myBrush, 3, 3)<BR><BR>' Set the content type and return the image<BR>Response.ContentType = "image/GIF"<BR>objBMP.Save(Response.OutputStream, ImageFormat.Gif)<BR><BR>' Kill our objects<BR>objFont.Dispose()<BR>objGraphics.Dispose()<BR>objBMP.Dispose()<BR>%><BR>You could loop through the pixels of the image and figure out where the black pixels stop being used. I assume that you are doing this as a one time shot (not constantly displaying dynamic images on your site!) This is pretty processor intensive. Pseudo code follows:<BR><BR>intRightmostBlackPixel = 0<BR>intBottommostBlackPixel = 0<BR>intTempColorValue = http://aspmessageboard.com/archive/index.php/0<BR><BR>For intYCoordinate = 1 To ImageHeight<BR> For intXCoordinate = 1 To ImageWidth<BR><BR> intTempColorValue = 0<BR> intTempColorValue = intTempColorValue + GetRedValue(intXCoordinate, intYCoordinate)<BR> intTempColorValue = intTempColorValue + GetGreenValue(intXCoordinate, intYCoordinate)<BR> intTempColorValue = intTempColorValue + GetBlueValue(intXCoordinate, intYCoordinate)<BR><BR> If intTempColorValue 3 < 128 Then<BR> ' The pixel is black<BR><BR> If intYCoordinate > intBottommostBlackPixel Then<BR> intBottommostBlackPixel = intYCoordinate<BR> End If<BR> If intXCoordinate > intRightmostBlackPixel Then<BR> intRightmostBlackPixel = intXCoordinate<BR> End If<BR><BR> End If<BR> Next<BR>Next<BR><BR>Image.Crop(intRightmostBlackPixel + intMargin, intBottommostBlackPixel + intMargin)There is a MeasureString function!<BR><BR>' =========================<BR><BR> Dim objFont As Font<BR> Dim objSize As SizeF<BR> Dim objBitmap As Bitmap<BR> Dim intWidth As Integer<BR> Dim intHeight As Integer<BR><BR> intWidth = 0<BR> intHeight = 0<BR><BR> objFont = New Font("Arial", 28)<BR> objBitmap = New Bitmap(1, 1, PixelFormat.Format32bppPArgb)<BR> objSize = Graphics.FromImage(objBitmap).MeasureString("Your Text Here", objFont)<BR><BR> intWidth = objSize.Width<BR> intHeight = objSize.Height<BR><BR><BR>That is exactly what I was looking for!!<BR>Works good.<BR>Thank you<BR>Aaron<BR><BR>
 
Back
Top