Click here to Skip to main content
15,887,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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; http://tinypic.com/view.php?pic=vrvymq&s=8#.VO3tbPmsWGw[^]
Posted
Comments
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.
—SA

1 solution

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:
http://stackoverflow.com/questions/10737399/accurately-measuring-rendering-hit-testing-and-printing-text-in-a-winforms-app[^].

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. :-)

—SA
 
Share this answer
 
Comments
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. :-(
—SA
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.

http://tinypic.com/view.php?pic=2mg3x34&s=8#.VO37zfmsWGw -- 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.
—SA
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. :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900