I am trying to use font Tahoma and measure the string with font size 25 pixels. I am then trying to draw the text in the
string s = "N 40º 34' 36.556\" W 70º 45' 24.412\"";
StringFormat tb = new StringFormat() { FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.NoClip | StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.FitBlackBox | StringFormatFlags.LineLimit,
Trimming = StringTrimming.None,
HotkeyPrefix = HotkeyPrefix.None };
tb.Alignment = StringAlignment.Far;
tb.LineAlignment = StringAlignment.Far;
Font font = new Font("Tahoma", 25, FontStyle.Regular, GraphicsUnit.Pixel);
SizeF measuredSize = graphics.MeasureString(allTexts, font, SizeF.Empty, tb);
graphics.DrawString(s, font, textBrush, textRect, tb);
the string is drawn but the ending qoute char is not drawn. Changing to another font (like Segoe UI) solves the issue but the measurement is now bigger than the drawn text making the background rectangle I am drawing for the text bigger on the left side than the text - like the line has padding or a space at the beginning. the background is not in sample code but it is drawn on the same rectangle
Any ideas?
@DanAvni
It seems you are experiencing issues with the MeasureString
method when using the Tahoma font at a size of 25 pixels. Specifically, the ending quote character is not being drawn, and when switching to another font like Segoe UI, the measured size becomes larger than the drawn text, causing alignment issues.
Here are a few suggestions to troubleshoot and potentially resolve the issue:
-
Font Compatibility: Ensure that the Tahoma font is properly installed and accessible in your environment. Sometimes, certain fonts may not render correctly due to compatibility issues.
-
StringFormat Settings: You are using a combination of StringFormatFlags
that may affect how the text is measured and drawn. Consider simplifying the StringFormat
settings to see if it resolves the issue. For example, you might want to start with just StringFormatFlags.NoWrap
and StringFormatFlags.MeasureTrailingSpaces
to see if that changes the behavior.
-
Text Measurement: When measuring the string, ensure that the SizeF.Empty
parameter is appropriate for your use case. You might want to specify a layout area that matches the expected drawing area to get a more accurate measurement.
-
Debugging Output: Add debugging output to log the measured size and the actual drawn size. This can help you identify discrepancies between the two and adjust your layout accordingly.
-
Testing with Different Fonts: Since you mentioned that switching to Segoe UI resolves the issue of the missing character but introduces padding, you might want to experiment with other fonts or adjust the text rectangle to accommodate the differences in font metrics.
Here is a simplified version of your code that you can use to test these suggestions:
string s = "N 40º 34' 36.556\" W 70º 45' 24.412\"";
StringFormat tb = new StringFormat() { FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.MeasureTrailingSpaces };
tb.Alignment = StringAlignment.Far;
tb.LineAlignment = StringAlignment.Far;
Font font = new Font("Tahoma", 25, FontStyle.Regular, GraphicsUnit.Pixel);
SizeF measuredSize = graphics.MeasureString(s, font, SizeF.Empty, tb);
graphics.DrawString(s, font, textBrush, textRect, tb);
// Log the measured size
Console.WriteLine($"Measured Size: {measuredSize}");
By following these steps, you should be able to identify the cause of the measurement inaccuracies and adjust your implementation accordingly. If the problem persists, consider reaching out to the Aspose support team with specific details about your environment and the issue for further assistance.
Testing with different string formats does not help; I need to measure the text size using SizeF.Empty to find the text size that will allow the whole text to fit in my image. so I am in a loop gradually decreasing it until the measured size fits in my image
Hello @DanAvni.
I suppose that Graphics.MeasureString
returns a SizeF which is less than necessary. That is why the last quote is not drawn.
Let me check it
@evgeniy.sidenko , I need to increase it by about 20 for the quote to render but this is just by trial an error not something concrete I can put in code (my code reduces the font size so for every font size the size will be different + the text rendered is different so it might be I need to add more if the last char is W)
@DanAvni
Yes, I confirm that we have a problem here. We will take it to work ASAP.
@DanAvni
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Our apologies for the inconvenience you are facing. This ticket is going to enter 25.05 release. As a workaround you might try using larger precalculated padding or check if older versions work in the expected way.
Issue ID(s): IMAGINGNET-7591
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.