I'm having trouble using MeasureCharacterRanges. If I use it to get the width of the string "TEST" I would expect to get the same width as I would if I used it to get the individual widths of the characters "T", "E", "S", and "T" and then added the resulting values together.
The problem is that the two values don't seem to match unless the Graphics.PageUnit is set to Inch. I don't understand why it doesn't work with the default setting of PageUnit.Display.
I have a procedure that calls MeasureCharacterRanges defined like so: -
-
private Region getCharRegion(Graphics g, string text)
-
{
-
g.PageUnit = GraphicsUnit.Inch;
-
StringFormat sf = new StringFormat { Trimming = StringTrimming.None, FormatFlags = (StringFormatFlags.NoClip | StringFormatFlags.NoWrap) };
-
sf.SetMeasurableCharacterRanges(new[] { new CharacterRange(0, text.Length) });
-
SizeF size = g.MeasureString(text, TextFont, (int)TextFont.Style);
-
RectangleF layoutRect = new RectangleF(0f, 0f, size.Width, size.Height);
-
Region[] regions = g.MeasureCharacterRanges(stringToMeasure, TextFont, layoutRect, sf);
-
g.PageUnit = GraphicsUnit.Display;
-
return regions[0];
-
}
-
I try to measure the strings using 3 methods as below but none of them produce matching results for the full string versus the sum of the characters. -
float w1 = getCharRegions(g, "TEST").GetBounds(g).Width;
-
float w2 = getCharRegions(g, "T").GetBounds(g).Width;
-
float w3 = getCharRegions(g, "E").GetBounds(g).Width;
-
float w4 = getCharRegions(g, "S").GetBounds(g).Width;
-
float w5 = getCharRegions(g, "T").GetBounds(g).Width;
-
float w6 = w2 + w3 + w4 + w5;
-
-
w1 = g.MeasureString("TEST", TextFont).Width;
-
w2 = g.MeasureString("T", TextFont).Width;
-
w3 = g.MeasureString("E", TextFont).Width;
-
w4 = g.MeasureString("S", TextFont).Width;
-
w5 = g.MeasureString("T", TextFont).Width;
-
w6 = w2 + w3 + w4 + w5;
-
-
w1 = TextRenderer.MeasureText("TEST", TextFont, new Size(0,0), TextFormatFlags.NoPadding | TextFormatFlags.NoFullWidthCharacterBreak).Width;
-
w2 = TextRenderer.MeasureText("T", TextFont, new Size(0, 0), TextFormatFlags.NoPadding).Width;
-
w3 = TextRenderer.MeasureText("E", TextFont, new Size(0, 0), TextFormatFlags.NoPadding).Width;
-
w4 = TextRenderer.MeasureText("S", TextFont, new Size(0, 0), TextFormatFlags.NoPadding).Width;
-
w5 = TextRenderer.MeasureText("T", TextFont, new Size(0, 0), TextFormatFlags.NoPadding).Width;
-
w6 = w2 + w3 + w4 + w5;
-
In all cases above i can't get w1 to equal w6.
Can anyone explain why this is so. I don't want my application to break in the future or on different system because I don't understand what is happening.
Thanks.
4 4844
Are you including the amount of spacing that would exist between the characters done up in a string?
I would think the graphical length of "T" + "E" would be less then "TE"
Thanks for the reply. In actual fact the opposite is true. The reported length of "T" + "E" is more than the length of "TE".
I'm not accounting for any spacing and not sure how I would go about it....
I think this has whole issue has something to do with hinting (I have the TextRenderingHint set to ClearTypeGridFit) but I don't have the knowledge to understand exactly what causes it. I can work around this, but the workarounds cause other problems in terms of performance. GDI+ is fast becoming a cumbersome pain..... :/
Did you ever find a solution to this problem?
@Plater
Unfortunately not a real solution, no, I've had to make do with fudging and hacking my way around it. It almost certainly has to do with font hinting but I didn't really have the time required to try and understand it.
By tweaking the routine to use cached bitmaps and only regenerate when requried I managed to get performance to an acceptable level, but I'd still be interested to hear solutions from anyone who has experience similar issues.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Charles Law |
last post by:
If I draw a rectangle, 6 inches by 2 inches, on a user control with a
PageUnit of Inches, I get a rectangle of 7 by 2.2 inches.
Is this what people would expect? I would have hoped that it was at...
|
by: Yaron |
last post by:
Hi,
I made a procedure which receives a Graphics parameter, and with that Graphics parameter, performs various things. The area of confusion here is that when I pass the procedure a Graphics...
|
by: johannblake |
last post by:
I am wondering whether it is easy to setup a coordinate system for
drawing (using GDI+) that uses meters (or any custom scaling for that
matter). Currently, I need to convert from pixels to meters...
|
by: Hadar |
last post by:
Hi,
I'm getting "object is currently in use elsewhere" when I use
System.Drawing.Graphics.MesureString.
This is what I do:
My controls use a utility class the helps it to mesure strings.
To...
|
by: Mark Davison |
last post by:
Hi,
I hope someone can help, this has been driving me mad! I am trying to
generate a graphic on the fly. This is then shown in the browser in a "print
preview" type window. The user can click a...
|
by: standish22 |
last post by:
Hey, I'm confused!
I have a document that I want to print to a printer. I'm using inches
as my units. I got it working w/ Pixels, but my boss told me I have
to use Inches instead.
My...
|
by: active |
last post by:
I have an image and a graphics object created (FromImage) from that image.
I need to create a new image and create a new graphics object from the new
image.
I want the new graphics object have...
|
by: alarock |
last post by:
HI
THIS IS MY CODE..I WANT TO ZOOM THE BITMAP..HOW IT IS POSSIBLE???
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;...
|
by: Rainer Queck |
last post by:
Hello NG,
I am trying to adjust the column width of a DataGridView to the Text in its
column header.
To do this I am using the Graphics.MeasureString method. Unfortunatly I
alway get a too...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |