Click here to Skip to main content
12,829,346 members (39,748 online)
Click here to Skip to main content
Add your own
alternative version

Stats

6.4K views
362 downloads
14 bookmarked
Posted 26 Jul 2015

GDI DrawString Configurator App

, 26 Jul 2015 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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.

  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.

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

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

  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.

  6. Clipping does not affect GraphicsPath.AddString method.

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)

Share

About the Author

Petr Bříza
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.


You may also be interested in...

Pro

Comments and Discussions

 
QuestionstringFormat settings and graphics settings Pin
Member 912454320-Jan-16 1:13
memberMember 912454320-Jan-16 1:13 
QuestionVS Incompatible... Pin
AchLog28-Jul-15 3:41
memberAchLog28-Jul-15 3:41 
Generalthanks Pin
Hooman_Kh26-Jul-15 12:09
professionalHooman_Kh26-Jul-15 12: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 26 Jul 2015
Article Copyright 2015 by Petr Bříza
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid