473,326 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Graphics.PageUnit and MeasureCharacterRanges

nukefusion
221 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     private Region getCharRegion(Graphics g, string text)
  3.         {
  4.             g.PageUnit = GraphicsUnit.Inch;
  5.             StringFormat sf = new StringFormat { Trimming = StringTrimming.None, FormatFlags = (StringFormatFlags.NoClip | StringFormatFlags.NoWrap) };
  6.             sf.SetMeasurableCharacterRanges(new[] { new CharacterRange(0, text.Length) });
  7.             SizeF size = g.MeasureString(text, TextFont, (int)TextFont.Style);
  8.             RectangleF layoutRect = new RectangleF(0f, 0f, size.Width, size.Height);
  9.             Region[] regions = g.MeasureCharacterRanges(stringToMeasure, TextFont, layoutRect, sf);
  10.             g.PageUnit = GraphicsUnit.Display;
  11.             return regions[0];
  12.         }
  13.  
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.

Expand|Select|Wrap|Line Numbers
  1.                 float w1 = getCharRegions(g, "TEST").GetBounds(g).Width;
  2.                 float w2 = getCharRegions(g, "T").GetBounds(g).Width;
  3.                 float w3 = getCharRegions(g, "E").GetBounds(g).Width;
  4.                 float w4 = getCharRegions(g, "S").GetBounds(g).Width;
  5.                 float w5 = getCharRegions(g, "T").GetBounds(g).Width;
  6.                 float w6 = w2 + w3 + w4 + w5;
  7.  
  8.                 w1 = g.MeasureString("TEST", TextFont).Width;
  9.                 w2 = g.MeasureString("T", TextFont).Width;
  10.                 w3 = g.MeasureString("E", TextFont).Width;
  11.                 w4 = g.MeasureString("S", TextFont).Width;
  12.                 w5 = g.MeasureString("T", TextFont).Width;
  13.                 w6 = w2 + w3 + w4 + w5;
  14.  
  15.                 w1 = TextRenderer.MeasureText("TEST", TextFont, new Size(0,0), TextFormatFlags.NoPadding | TextFormatFlags.NoFullWidthCharacterBreak).Width;
  16.                 w2 = TextRenderer.MeasureText("T", TextFont, new Size(0, 0), TextFormatFlags.NoPadding).Width;
  17.                 w3 = TextRenderer.MeasureText("E", TextFont, new Size(0, 0), TextFormatFlags.NoPadding).Width;
  18.                 w4 = TextRenderer.MeasureText("S", TextFont, new Size(0, 0), TextFormatFlags.NoPadding).Width;
  19.                 w5 = TextRenderer.MeasureText("T", TextFont, new Size(0, 0), TextFormatFlags.NoPadding).Width;
  20.                 w6 = w2 + w3 + w4 + w5;
  21.  
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.
Nov 15 '08 #1
4 4844
Plater
7,872 Expert 4TB
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"
Nov 17 '08 #2
nukefusion
221 Expert 100+
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..... :/
Nov 17 '08 #3
Plater
7,872 Expert 4TB
Did you ever find a solution to this problem?
Dec 5 '08 #4
nukefusion
221 Expert 100+
@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.
Dec 9 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
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...
1
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...
6
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...
1
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...
0
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...
0
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...
6
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...
0
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;...
6
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
0
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...
1
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)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.