Graphics.DrawString wie Label

  • VB.NET

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von Bluespide.

    Graphics.DrawString wie Label

    Hi,

    mir ist gerade aufgefallen, dass mein Text, den ich mit Graphics.DrawString Zeichne unschärfer aussieht als der von einem Label. Beides mal benutze ich die selbe Font und Farbe.

    Font: Calibri, 11

    VB.NET-Quellcode

    1. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    2. e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

    Bekommt man das irgendwie so hin, wie es das Label macht?

    Label oben, DrawString unten.
    Bilder
    • 4356789.png

      3,04 kB, 376×195, 170 mal angesehen
    Das ist doch wegen dem AntiAliasing.
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    Für die höchste Qualität setze folgende Einstellungen:

    VB.NET-Quellcode

    1. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
    2. e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit
    3. e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality

    VB.NET-Quellcode

    1. Protected Override Sub OnPaint(e As PaintEventArgs)
    2. Me.Animate()
    3. Dim rectangle As Rectangle = LayoutUtils.DeflateRect(MyBase.ClientRectangle, MyBase.Padding)
    4. ImageAnimator.UpdateFrames()
    5. Dim image As Image = Me.Image
    6. If image IsNot Nothing Then
    7. Me.DrawImage(e.Graphics, image, rectangle, MyBase.RtlTranslateAlignment(Me.ImageAlign))
    8. End If
    9. Dim hdc As IntPtr = e.Graphics.GetHdc()
    10. Dim nearestColor As Color
    11. Try
    12. Using Dim windowsGraphics As WindowsGraphics = WindowsGraphics.FromHdc(hdc)
    13. nearestColor = windowsGraphics.GetNearestColor(If(MyBase.Enabled, Me.ForeColor, MyBase.DisabledColor))
    14. End Using
    15. Finally
    16. e.Graphics.ReleaseHdc()
    17. End Try
    18. If Me.AutoEllipsis Then
    19. Dim clientRectangle As Rectangle = MyBase.ClientRectangle
    20. Dim preferredSize As Size = Me.GetPreferredSize(New Size(clientRectangle.Width, clientRectangle.Height))
    21. Me.showToolTip = (clientRectangle.Width < preferredSize.Width OrElse clientRectangle.Height < preferredSize.Height)
    22. Else
    23. Me.showToolTip = False
    24. End If
    25. 'UseCompatibleTextRendering ist Standardmäßig false!
    26. If Me.UseCompatibleTextRendering Then
    27. Using Dim stringFormat As StringFormat = Me.CreateStringFormat()
    28. If MyBase.Enabled Then
    29. Using Dim brush As Brush = New SolidBrush(nearestColor)
    30. e.Graphics.DrawString(Me.Text, Me.Font, brush, rectangle, stringFormat)
    31. GoTo IL_15B
    32. End Using
    33. End If
    34. ControlPaint.DrawStringDisabled(e.Graphics, Me.Text, Me.Font, nearestColor, rectangle, stringFormat)
    35. IL_15B:
    36. GoTo IL_1BF
    37. End Using
    38. End If
    39. 'Mit dem TextRenderer....
    40. Dim flags As TextFormatFlags = Me.CreateTextFormatFlags()
    41. If MyBase.Enabled Then
    42. TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, rectangle, nearestColor, flags)
    43. Else
    44. Dim foreColor As Color = TextRenderer.DisabledTextColor(Me.BackColor)
    45. TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, rectangle, foreColor, flags)
    46. End If
    47. IL_1BF:
    48. MyBase.OnPaint(e)
    49. End Sub


    So macht ein Label das^^ Also wie du sagtest mit einem TextRenderer (außer wenn UseCompatibleTextRendering=true). Habe den Code mit dem ILSpy ausgelesen - ob die da wirklich GoTo's verwendet haben oder das nur ein 'Bug' ist??