Click here to Skip to main content
15,881,882 members
Articles / Multimedia / GDI+
Tip/Trick

GDI DrawString Configurator App

Rate me:
Please Sign up or sign in to vote.
4.90/5 (16 votes)
26 Jul 2015CPOL2 min read 25.1K   1K   22   6
This app helps you with understanding and using GDI DrawString function: precise measuring, positioning, quality setting and performance tuning.

Introduction

I presume you have already encountered the GDI DrawString function - in .NET, it is represented by System.Drawing.Graphics.DrawString method. In most cases, it is easy and straightforward, but in some advanced cases, you might find yourself struggling with combinations of various flags and settings to get the desired result. This app could be your tool to do this experimenting quickly and easily.

Image 1

Using the App

This is a brief list of functionalities you might find useful in the code:

  • Basic usage of DrawString method with basic settings (font, size, style).
  • Working with font families: listing, using generic families, determining various style support by font families, observing impact of selected font to positioning.
  • Testing text clipping and trimming by resizing the window so the whole text cannot fit to the specified area
  • Comparing the result with GraphicsPath.AddString method used for drawing text outlines.
  • Basic text output measuring using MeasureString and MeasureCharacterRanges methods and observing impact of various flags to the results.
  • Measuring and computing more font metrics such as leading or ascent (text baseline).
  • Experimenting with various flags and settings of StringFormat object.
  • Impact of text-related properties of Graphics object to text rendering quality.
  • Benchmarking the rendering performance using different settings.

Points of Interest

I have discovered some interesting things during development of this app and am using it for my purposes while developing another application.

  1. DrawString method adds some padding on the left side and right side of the text. If you want the text to start precisely on some X coordinate, you need to measure this padding and adjust your start point accordingly. MeasureString doesn't help here, you need to use MeasureCharacterRanges.

    Image 2

  2. If you want to be able to position characters really precisely, just using MeasureCharacterRanges is still not good enough, because the result numbers are rounded. You need to also activate the NoClip flag to get the precise floating-point numbers.

    Image 3

  3. The characters do not hesitate to draw also outside their measured ranges. Maybe this is the reason for using text padding.

    Image 4

  4. Yes, GDI handles kerning, as you can see in the following picture:

    Image 5

  5. If you experiment with drawing text outline, you will discover it could be sometimes misaligned with standard DrawString. Sometimes, it depends solely on the font size. If the texts are misaligned, it seems that MeasureString matches with GraphicsPath.AddString method (outlined text) while MeasureCharacterRanges goes precisely with standard DrawString method.

    Image 6

  6. Clipping does not affect GraphicsPath.AddString method.

    Image 7

I am sure you will find out lot of things on your own. I hope this app will help with solving your problems. Good luck!

History

  • v 1.0 - Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Petr Bříza
Czech Republic Czech Republic


Freelance .NET developer from Prague, Czech republic. You have a problem - I am the solution.

Visit me at http://petr.briza.net.


Comments and Discussions

 
QuestionDraw String as seen on TextBox Multiline and single Line also RTL Or LTR For Both English And Arabic Pin
Member 384671514-Mar-24 8:25
Member 384671514-Mar-24 8:25 
Suggestionto bypass ranges count limitation Pin
Dev Tools4-Mar-19 4:53
Dev Tools4-Mar-19 4:53 
Thanks, nice work. Here is code to to bypass ranges count limitation (translated from vb.net)
private void InternalDrawCharactedRanges(Graphics g, Rectangle rect)
{
    // Dim charRanges As CharacterRange()
    // If Me.CanvasText.Length > 32 Then
    // ' more than 32 ranges not allowed for SetMeasurableCharacterRanges
    // charRanges = New CharacterRange() {New CharacterRange(0, Me.Text.Length)}
    // Else
    // charRanges = New CharacterRange(Me.CanvasText.Length - 1) {}
    // For i As Integer = 0 To Me.Text.Length - 1
    // charRanges(i) = New CharacterRange(i, 1)
    // Next
    // End If

  
    List<CharacterRange[]> charRangeArrList = new List<CharacterRange[]>();
    List<CharacterRange> rngList = new List<CharacterRange>();
    int count = 0;
    for (int i = 0; i <= this.Text.Length - 1; i++)
    {
        if (i - count < 32)
            rngList.Add(new CharacterRange(i, 1));
        else
        {
            charRangeArrList.Add(rngList.ToArray());
            rngList = new List<CharacterRange>();
            i -= 1;
            count = i;
        }
    }
    if (rngList.Count > 0)
        charRangeArrList.Add(rngList.ToArray());

    // ---
    for (int i = 0; i <= charRangeArrList.Count - 1; i++)
    {
        CharacterRange[] charRanges = charRangeArrList[i];
        StringFormat xStringFormat = this.GetStringFormat;
        xStringFormat.SetMeasurableCharacterRanges(charRanges);
        System.Drawing.Region[] ranges = g.MeasureCharacterRanges(this.Text, this.Font, rect, xStringFormat);
        foreach (System.Drawing.Region region in ranges)
        {
            RectangleF charRect = region.GetBounds(g);
            g.DrawRectangle(Pens.Green, charRect.X, charRect.Y, charRect.Width, charRect.Height);
        }
    }
}


modified 4-Mar-19 16:12pm.

Generalres Pin
Member 1182388517-Nov-17 19:11
Member 1182388517-Nov-17 19:11 
QuestionstringFormat settings and graphics settings Pin
Alex M.H.20-Jan-16 0:13
Alex M.H.20-Jan-16 0:13 
QuestionVS Incompatible... Pin
AchLog28-Jul-15 2:41
AchLog28-Jul-15 2:41 
Generalthanks Pin
Hooman_Kh26-Jul-15 11:09
Hooman_Kh26-Jul-15 11:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.