Text2Image RRS feed

  • Question

  • User-1468470916 posted
    *** edited by: master4eva *** Please enclose your code in < code ></ code> tags (without the spaces). This will make your code easier to read online; therefore, encouraging a response to be faster. It is to your own benefit for your question to be answered in future. I have already done the editing to include the < code ></ code> tags for this post. ********* For a project I do need to write some images with a fixed font, size and width of the image, I am having no problem with writing the image with the following snippet: Private Function CreateImage(ByVal sImageText As String) As Bitmap 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(Me.FontFamily, Me.FontSize, Me.FontStyle, System.Drawing.GraphicsUnit.Point) ' Create a graphics object to measure the text's width and height. Dim MyGraphics As Graphics = Graphics.FromImage(bmpImage) ' This is where the bitmap size is determined. iWidth = Fix(MyGraphics.MeasureString(sImageText, MyFont).Width) iHeight = Fix(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.Transparent) does not work MyGraphics.Clear(Me.BackgroundColor) MyGraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit MyGraphics.CompositingQuality = Drawing2D.CompositingQuality.HighQuality MyGraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality MyGraphics.DrawString(sImageText, MyFont, New SolidBrush(Me.TextColor), 0, 0) MyGraphics.Flush() Return bmpImage End Function But my problem is that the image must not be larger than the given width and there should be added line-breaks if the image is larger than the given width ==> so the width should remain and the height should be adapted ... I have already tried parsing the string and measuring again and again but the string-measuring seams to be not really precise?? Does somebody have any ideas on how to do this the best way? Thanks in advance Greetings Stefan
    Wednesday, May 19, 2004 9:48 AM

All replies

  • User-865881720 posted
    When I want to create a text image that is constrained by a desired width I use the overloaded method of Graphics.MeasureString which allows a width parameter AND a StringFormat parameter as follows where MyWidth is the desired width: Dim SF As StringFormat SF = New StringFormat(StringFormatFlags.LineLimit OR StringFormatFlags.NoClip) SF.Alignment = StringAlignment.Center 'or .Far or .Near as desired Dim StrSizeF As SizeF = MyGraphics.MeasureString(sImageText, MyFont, MyWidth, SF) Dim IWidth As Integer = CInt(StrSizeF.Width) Dim IHeight As Integer = CInt(StrSizeF.Height) I also find that I get the best text rendering with the following: MyGraphics.TextRenderingHint = TextRenderingHint.AntiAlias MyGraphics..SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    Wednesday, May 19, 2004 10:28 PM
  • User-1468470916 posted
    Thank you very much for your answer!!! I'll try that and post the result here ... Greetings Stefan
    Friday, May 21, 2004 12:26 PM