Click here to Skip to main content
14,382,476 members
Rate this:
Please Sign up or sign in to vote.
Hi everyone,

I need to get Text Width in Pixel. But TextRenderer.MeasureText() and Graphics.MeasureString() methods aren't work correctly.

So I found the C++ method name of the "gettextextentpoint32". But I think, I couldn't use this method correctly. With this method some words doesn't measure correctly.
I need to measure word like; Width of text in italic font[^] like this.

Please help me. Can you suggest any other way or true way...

I can write C++ DLL for use my C# project. So, you can answer with C++ languague. Thanks for all answer. Have a good day.

My problem with image;[^]
Sergey Alexandrovich Kryukov 25-Feb-15 11:13am
You are right. This is a very annoying problem. The lack of accuracy is related to font hinting. I found some more complicated solution on the forums, only to find out that it was better, but still not perfect. I would come up with design not relying on the possibility to get exact width.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question. Sorry, but at this moment, I know only two solutions, but both are of low quality. There is a claim that accurate solution exist, but please see this:[^].

The second solution is just quite awkward and would provide poorest performance, very likely prohibitively poor: draw the string on some bitmap starting with x = 0 in contrast color, say, white on black, and then detect rightmost non-black pixel.

Sorry: so far, so bad. :-)

Umut Comlekcioglu 25-Feb-15 11:29am
Like you say, second solution have very poorest performance. So I can't use it in my project.

First solution is not solve my problem. I tested them before. :(

Thanks for all helps. I'm waiting for your comments.
Sergey Alexandrovich Kryukov 25-Feb-15 11:38am
What comments? Of course you are right. I warned from the very beginning that I don't know better solution. You can only hope that someone knows something better, but I would not hold my breath. I'm afraid the only real solution I would suggest is this: change you design to the design not relying upon the possibility to get accurate text width measurements.
I'm serious: if I'm not much mistaken, this problem lasted for years, so I have pretty low hope that I'm missing something. Let's see if someone could correct me. :-(
Umut Comlekcioglu 25-Feb-15 11:45am
I guess, finally I'm done it. Now, I will try that solution with other computer. I hope it's will work. Thanks for link and other everything. -- I think, this measurement is enough?
Sergey Alexandrovich Kryukov 25-Feb-15 11:54am
Please do many people, including myself, the big favor: publish that solution as CodeProject Tip/Trick (if not the full article, of course).
If you do, please notify me by commenting this comment. Just the reference to existing solution can also do, if this is a solution, not a picture.
Umut Comlekcioglu 25-Feb-15 11:59am
Okay I will be publish solution tomorrow. But, I need to test it other computers and resolutions. If test results will correct, I will publish it. :)
Sergey Alexandrovich Kryukov 25-Feb-15 12:59pm
Great. Thank you in advance. Please leave me a note by another comment.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100