none
DrawString looks garbled. RRS feed

  • Question

  • I use Visual Studio 2017 VB.NET for drawing wrapmode text in the Form.
     In the some cases, the drawing text looks garbled.
     Sample code:

    Public Class Form1
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            Dim sf As StringFormat = New StringFormat
            Dim fnt As Font = New Font("Arial", 10, FontStyle.Underline)
            Dim br As Brush = SystemBrushes.ControlText
            sf.Alignment = StringAlignment.Far
            e.Graphics.DrawString("City:", fnt, br, New RectangleF(0, 0, 90, 40), sf)
            sf.Alignment = StringAlignment.Near
            e.Graphics.DrawString("Zeebrugge, Brugge", fnt, br, New RectangleF(91, 0, 90, 40), sf)
        End Sub
    End Class

    Tuesday, August 14, 2018 4:32 AM

All replies

  • I am using VB.NET  to draw two strings in wrap mode. The drawing text looks garbled.
    Sample code:

    Public Class Form1
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            Dim sf As StringFormat = New StringFormat
            Dim fnt As Font = New Font("Arial", 10, FontStyle.Underline)
            Dim br As Brush = SystemBrushes.ControlText
            sf.Alignment = StringAlignment.Far
            e.Graphics.DrawString("City:", fnt, br, New RectangleF(0, 0, 90, 40), sf)
            sf.Alignment = StringAlignment.Near
            e.Graphics.DrawString("Zeebrugge, Brugge", fnt, br, New RectangleF(91, 0, 90, 40), sf)
        End Sub
    End Class

    • Merged by Stanly Fan Wednesday, August 15, 2018 1:46 AM the same thread
    Tuesday, August 14, 2018 5:06 AM
  • Hi,

    When the DrawString function is called, a border rectangle parameter is passed in, and the text will automatically wrap within this range.
    Because the number of words is variable, the height of this rectangle should also change dynamically.
    code :

     Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
            Dim sf As StringFormat = New StringFormat
            Dim fnt As Font = New Font("Arial", 10, FontStyle.Underline)
            Dim br As Brush = SystemBrushes.ControlText
            sf.Alignment = StringAlignment.Far
            e.Graphics.DrawString("City:", fnt, br, New RectangleF(0, 0, 90, 40), sf)
            sf.Alignment = StringAlignment.Near
            'e.Graphics.DrawString("Zeebrugge, Brugge", fnt, br, New RectangleF(91, 0, 90, 40), sf)
            Dim descRect As RectangleF = New RectangleF()
            Using useFont As Font = New Font("Arial", 10, FontStyle.Underline)
                descRect.Location = New Point(91, 0)
                descRect.Size = New Size(600, (CInt(e.Graphics.MeasureString("Zeebrugge, Brugge", useFont, 600, StringFormat.GenericTypographic).Height)))
                e.Graphics.DrawString("Zeebrugge, Brugge", useFont, Brushes.Black, descRect)
            End Using
    
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 14, 2018 5:10 AM
  • Try the alternative (instead of DrawString):

    TextRenderer.DrawText(e.Graphics, "City:", fnt, New Rectangle(0, 0, 90, 40), SystemColors.ControlText, TextFormatFlags.Right Or TextFormatFlags.Top Or TextFormatFlags.NoPrefix)
    TextRenderer.DrawText(e.Graphics, "Zeebrugge, Brugge", fnt, New Rectangle(91, 0, 90, 40), SystemColors.ControlText, TextFormatFlags.Left Or TextFormatFlags.Top Or TextFormatFlags.NoPrefix Or TextFormatFlags.WordBreak)
    


    Tuesday, August 14, 2018 5:18 AM
  • Thank you for replay, but i need to draw text in wrap mode without changing the width of rectangle.
    • Edited by avoriv Tuesday, August 14, 2018 5:22 AM
    Tuesday, August 14, 2018 5:19 AM
  • TextRenderer.DrawText does not work with right aligment.

    On some machines it works.

    Tuesday, August 14, 2018 5:36 AM
  • Thank you for replay, but i need to draw text in wrap mode without changing the width of rectangle.

    Hi,

    The width of the rectangle does not change. What do you want to do beyond the rectangular part? What is the effect you want?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 14, 2018 6:57 AM
  • Hello, Alex.

    I meant, that you change rectangle width in your code.

    descRect.Size = New Size(600, (CInt( raphics.MeasureString("Zeebrugge, Brugge", useFont, 600, StringFormat.GenericTypographic).Height)))
                e.Graphics.DrawString("Zeebrugge, Brugge", useFont, Brushes.Black, descRect)


    • Edited by avoriv Tuesday, August 14, 2018 8:13 AM
    Tuesday, August 14, 2018 7:28 AM
  • Sorry, Viorel.

            

    TextRenderer.DrawText works with right alignment.

    How can i set GenericTypographic mode for TextRender (GDI)?
    How can i set WordBreak but without cut like in GDI+ (DrawString)?



    • Edited by avoriv Tuesday, August 14, 2018 8:12 AM
    Tuesday, August 14, 2018 7:44 AM
  • You can probably clear it up by setting one or more of the following Graphics properties before performing the drawing:

    • CompositingQuality
    • InterpolationMode
    • SmoothingMode
    • TextRenderingHint

    e.g.

    e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
    e.Graphics.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by LeonCS Thursday, August 16, 2018 9:37 AM
    Tuesday, August 14, 2018 1:14 PM
    Moderator
  • Hello Reed,
    I tried your suggestion. It still is not same as in GDI+.
    Wednesday, August 15, 2018 3:21 PM
  • Use the overload of DrawString that takes a StringFormat and pass StringFormat.GenericTypographic for the parameter value.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, August 15, 2018 3:48 PM
    Moderator
  • The problem is that drawing text using DrawString looks garbled. See my question on top.
    Wednesday, August 15, 2018 4:30 PM
  • The problem is that drawing text using DrawString looks garbled. See my question on top.

    Did you try the GenericTypographic string format and then customizing the layout as needed?

    Instead of:

    Dim sf As StringFormat = New StringFormat

    Use

    Dim sf As StringFormat = StringFormat.GenericTypographic.Clone

    Then change alignment etc. as needed.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, August 16, 2018 4:47 AM
    Moderator
  • My question was:
    How can i set GenericTypographic mode for TextRender (GDI) to use DrawText function?
    Thursday, August 16, 2018 5:53 AM
  • I have no experience using underlined - - but - - have you tried using a modern font like Segoe UI ?

    Burn all valuable data to CD/DVD disks. EMP proof.

    Thursday, August 16, 2018 9:40 AM
  • Segoe UI works --but this is not solution for me.
    Thursday, August 16, 2018 12:08 PM
  • Segoe UI works --but this is not solution for me.

    Good one Leon! Must be the true type clear screen or whachacallit?

    Avoriv,

    Is this a high resolution monitor? Are you seeing the dpi problem as in this thread.

    Thursday, August 16, 2018 12:49 PM
  • Workaround (by cheong00).

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Dim sf As StringFormat = New StringFormat(StringFormat.GenericTypographic)
        Dim fnt As Font = New Font("Arial", 10, FontStyle.Underline)
        Dim br As Brush = New SolidBrush(Color.Black)
        sf.Alignment = StringAlignment.Far
        e.Graphics.DrawString("City:" & Chr(0), fnt, br, New RectangleF(0, 0, 90, 100), sf)
        sf.Alignment = StringAlignment.Near
        e.Graphics.DrawString("Zeebrugge, Brugge" & Chr(0), fnt, br, New RectangleF(91, 0, 50, 100), sf)
     End Sub


    • Edited by avoriv Thursday, August 16, 2018 5:13 PM
    Thursday, August 16, 2018 5:09 PM
  • Hi,

    Do you resolve the issue? 

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by avoriv Monday, August 20, 2018 12:38 PM
    • Unmarked as answer by avoriv Thursday, June 20, 2019 7:56 AM
    Monday, August 20, 2018 10:05 AM
  • Microsoft does not resolve this issue.
    Wednesday, May 29, 2019 9:21 AM