 |
|
 |
There will be no problems if
1. SetTextRenderingHint(TextRenderingHintAntiAlias)
2. MeasureString(...)
|
|
|
|
 |
|
 |
Neither I have, is use gdi+ api directly on my C++ application, and I use GdipStringFormatGetGenericTypographic() to get a stringformat that don't give these 'extra' width when I measure...
|
|
|
|
 |
|
 |
Add the following line after format.SetMeasurableCharacterRanges
format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
And the more elegant solution will measure strings correctly even if they have trailing spaces.
Thanks for the code.
|
|
|
|
 |
|
 |
Works great, thanks both of you
|
|
|
|
 |
|
 |
Hello Pierre,
Just wondered whether you need to account for the added "|" used during your width search? Otherwise you will end up with a width that includes the "|". Correct?
Best Regards,
Jason
|
|
|
|
 |
|
 |
If you want to use this code, you should compute the difference of length between the empty string and the string which has to be measured. This should, in effect, return the length of the string without the "|" itself.
Pierre
|
|
|
|
 |
|
 |
I was trying to create a custom edit control. I tried to move the "cursor" by calculating the position using MeasureString then drawing the line for the cursor. It wasn't event close.
Then I tried MeasureString with StringFormat.GenericTypographic, and it was closer, but still WAY off.
Then I tried your MeasureDisplayStringWidth. Bam! Dead on.
Thanks!!!!
Sean
|
|
|
|
 |
|
 |
We have a textbox control that used a fixed width (monospaced) font and it was all good to make a table but now that we have to use unicode for asia languages it doe snot line up.
I was thinking to measure the string and the pad with spaces. I'd probablt hae to put a dummy period at the end of the string with the spaces to measure te whole length and then remove the dummy space.
any ideas for vb6?
thanks in advance.
codium
|
|
|
|
 |
|
 |
Hi all
I have a problem, a using a RichTextBox to get in put string from user.
User can Input string in to RichTextBox with different fonts. I want to get the width of the text in RichTextBox but i don't know how to do.
If using MeasureString then it only measyre for special font, but in this case the string have different fonts.
Have some one help me
|
|
|
|
 |
|
 |
Thanks a lot for your article! It helped a lot
|
|
|
|
 |
|
 |
I have another odd ball issue related to this code too. For example... "www.digitial-string-measure.com"
Generates odd width length as well. HELP! LOL By the way, non-integer font does really whack the results, in the case above, it just makes the issue worse.
Changing the string-format to typograhic had no effect, subtracting .right - .left had not effect... but this is reasonable because .left always seems to be zero in my experience.
|
|
|
|
 |
|
 |
Hi, I'm very greatful this code was posted here because it is exactly what I need. However, my IE seems to have a strange behavior and I really don't know where it comes from;
If I just have a simple string showing on an HTML page and compare it with the result I should get, the string on the page is a little bit smaller than the int returned by the mothod. However, if I just type the same text in a word document (viewed at 100% of course), then the result returned is indeed exactly the right size of the actual text...
What could this be? I'm a biginner btw, so I need little words :P
thx!
|
|
|
|
 |
|
 |
I tested 2 funcs with strings like: "iiiiiiiiiiiiiiiii", "cccccccccccccccccc", "lllllllllllllllll"
seem that their widths are incorrect.
Please double-check and give ideas?
-Sonny
|
|
|
|
 |
|
 |
Could someone please give an example of how to call this function please?
I don't understand what is needed from the graphics argument.
and the code sounds like exactly what I need
regards
|
|
|
|
 |
|
 |
Painting text requires a System.Drawing.Graphics argument, which is available in an OnPaint method override through the e.Graphics property (e is the argument of type System.Windows.Forms.PaintEventArgs).
My MeasureString replacement functions require you to pass the very same e.Graphics argument and the font you plan to use.
I hope this explains it...
Pierre
|
|
|
|
 |
|
 |
I am trying to find the width in pixels of a string that I pull from a database. I call a function printMessage() in the aspx page to grab the string. If the string width was bigger than my div, I wanted to insert a <br />. I am confused. How would I use the Graphics argument? Thank you.
|
|
|
|
 |
|
 |
What if I measured "Hello---"?
The code trawls across the bottom
of the bitmap from the right looking for the
first non-white pixel. Wont it hit the bottom
of the 'o' rather than find the last '-'?
Shouldn't scan from top to bottom as well?
...Or did I just read that code all wrong?..
Matt
|
|
|
|
 |
|
 |
The code scans the pixels, that's right. But it adds an additional vertical bar at the end of the text; it then looks for the last pixels in the image, which will be those of that additional character. So everything should just work
Pierre
|
|
|
|
 |
|
 |
Thanks for the article - solved a challenge my customer gave me to get text centered on a menu bar. Great approach to work around the MeasureString problem, good explanation of just why it is a problem. And the code worked perfectly first time copied in. Thanks again!
|
|
|
|
 |
|
 |
Is there any way to determine the number of lines used when: a text string was drawn inside a Rectangle?
MyGraphics.DrawString(DisplayText, MyFont, New SolidBrush(Color.White), drawTextRect, format)
The number of lines used is needed to vertical align the text in a box.
|
|
|
|
 |
|
 |
Thanks, works great. For those working in C, here's a quick adaptation for you to cut and paste. You may want to add some error checking:
void MeasureStringWidth(Gdiplus::Graphics *pG, const CString& strMeasureMe, Gdiplus::Font *pFont, Gdiplus::RectF *pRect)
{
Gdiplus::StringFormat format;
Gdiplus::RectF rect(0.0f, 0.0f, 1000.0f, 1000.0f);
Gdiplus::CharacterRange range(0, strMeasureMe.GetLength());
Gdiplus::Region region;
format.SetMeasurableCharacterRanges(1, &range);
pG->MeasureCharacterRanges(CT2CW(strMeasureMe), strMeasureMe.GetLength(), pFont, rect, &format, 1, ®ion);
region.GetBounds(&rect, pG);
*pRect = rect;
}
|
|
|
|
 |
|
 |
Thanks for the translation!
For some small strings I got weird results, but after setting string format to GenericTypographic it worked out well.
like..
const StringFormat* pGenericFormat = StringFormat::GenericTypographic();
StringFormat format = pGenericFormat;
format.SetMeasurableCharacterRanges(1, &range);
|
|
|
|
 |
|
 |
I have a label control. inside that label control i drwn a rectangle. inside this rectangle i wrote a string (eg : " i am the string ") using drawstring.
Now i would like to highlight the text area (" i am the string ") with some color. Do u have any idea ?
Sreejith Nair
[ My Articles ]
|
|
|
|
 |
|
 |
Hi,
I noticed that both of functions mentioned in this article didn't worked for me. Well, I was using a non installed font and getting wrong height of the output image. The same is with installed font though, but that is less.
Here is the simple code I used.
bmpCanvas = New Bitmap(1, 1)
Dim gr As Graphics = Graphics.FromImage(bmpCanvas)
Dim textSize As SizeF
textSize = gr.MeasureString(Text, CreateFont, Int32.MaxValue, StringFormat.GenericTypographic)
Though I was not using last two parameters of MeasureString but after reading one post from the same article, I tried but of no use. Its returning wrong height (less than the actual height). Here is the CreateFont method.
Dim mFont As Font
Dim mFontStyle As FontStyle
If Me.FontBold Then mFontStyle += FontStyle.Bold
If Me.FontItalic Then mFontStyle += FontStyle.Italic
If Me.FontStrikeThrough Then mFontStyle += FontStyle.Strikeout
If Me.FontUnderLined Then mFontStyle += FontStyle.Underline
If Me.UsePrivateFont Then
If Me.PrivateFontPath = "" Then Throw New Exception("Private Font File is missing")
Dim mFontCol As New PrivateFontCollection
mFontCol.AddFontFile(Me.PrivateFontPath)
Try
mFont = New Font(mFontCol.Families(0), Me.FontSize, mFontStyle)
Catch ex As Exception
'Failed to load private font, use standard instead of private
Me.UsePrivateFont = False
Me.FontName = "Arial"
Return CreateFont(True)
End Try
Else
mFont = New Font(Me.FontName, Me.FontSize, mFontStyle, GraphicsUnit.Pixel)
End If
Return mFont
Any idea if I am doing something wron ?
thanks in advance.
Sameers (theAngrycodeR)
theangrycoder@yahoo.com
http://www.theangrycoder.com
|
|
|
|
 |
|
 |
I dont see why you need to add 1 there. I found that a bounding rectangle of width 501 pixels just enough to print 67 characters with courier 9 has a resulting rectangle of Left:2,Width:499
btw, this method doesn't measure string width right all the time, I mean, using this method on a 62-char string and a 10-char string (both with same monospace font) doesn't give the same char width...
|
|
|
|
 |