Re: Graphics.MeasureString? or Graphics.DrawString?



Sorry for this late reply; I just returned from vacation.

It looks like MeasureString is pretty much useless in Vista.

You are right that my code is inconsistent in using this.Font and
legendFont, but this is only a cosmetic defect. I have one and only one
assignment:
Font legendFont = this.Font;
and I should not have used this.Font elsewhere.


"Ron" <rallen@xxxxxxxxxxxxxxxxx> wrote in message
news:85621930-2D13-466B-82AE-32D08BD5E8F2@xxxxxxxxxxxxxxxx
Norman,
I don't know why this would happen but I use the
StringFormat.GenericTypographic all through my utilities library for
printing and it works just fine for centering, left aligning, etc. I only
mentioned the Transform because that is why you need a position to measure
from. I do note that you are measuring with this.Font and then drawing
with legendFont though which may produce differnent results if the two
Font objects are different.

Ron Allen
"Norman Diamond" <ndiamond@xxxxxxxxxxxxxxxx> wrote in message
news:esQqIY3QIHA.748@xxxxxxxxxxxxxxxxxxxxxxx
Bad news.

With your suggested changes to the MeasureString call, in Vista it now
loses the last 2 characters of each legend. The simplest version of
Measure String only lost the last 1 character of each legend, in Vista.

Rebooting to XP on the same machine, with your suggested changes to the
MeasureString call, it again loses the last 2 characters of each legend.
The simplest version of MeasureString didn't lose any characters at all
from the legends in XP.


"Ron" <rallen@xxxxxxxxxxxxxxxxx> wrote in message
news:A3CC59CC-B04F-4D43-8299-F668AB568052@xxxxxxxxxxxxxxxx
Norman,
The measurements of the string depend on the StringFormat being used.
The default one includes a fair amount of space around the whole string
when measuring. For what you want to do one of the MeasureString calls
including a StringFormat would probably work best and include a clone of
StringFormat.GenericTypographic to get the closest meaurements around
the object being measured.
i.e. the following after you compute the starting point
legendSize = m_grChartResults.MeasureString(legend, this.Font,
new PointF(0, 0), StringFormat.GenericTypographic);
for a string at the top/left of the page. The starting location is used
because if there is some kind of non-linear transform done on the
graphics being used the size of the string depends on the location.

Ron Allen


"Norman Diamond" <ndiamond@xxxxxxxxxxxxxxxx> wrote in message
news:%23KwI8mFQIHA.1164@xxxxxxxxxxxxxxxxxxxxxxx
In Windows XP, the following code figures out where to position a
string and draws the string. In Vista, the following code figures out
where to position all except the last character of the string and draws
all except the last character of the string.

Notice that this legend is located near the lower left of the chart and
there's lots of blank space on the right. (In fact after all that
blank space there's another legend at the lower right which suffers
from exactly the same defect, but I'm omitting that code because the
positioning might lead to a red herring. Anyway there's lots and lots
of blank space where there should be the last character and lots and
lots of blank space.)

legendSize = m_grChartResults.MeasureString(legend, this.Font);
legendRectangle.X = ChartLeftMargin - (legendSize.Width / 2.0f);
if (legendRectangle.X < 0)
{
legendRectangle.X = 0;
}
legendRectangle.Width = legendSize.Width;
legendRectangle.Y = pbChartResults.Height - legendSize.Height - 2;
legendRectangle.Height = legendSize.Height;
m_grChartResults.DrawString(legend, legendFont, legendBrush,
legendRectangle);


DotNet Framework 2 with SP1, Visual Studio 2005 with SP1 C#.




.



Relevant Pages

  • Re: Graphics.MeasureString? or Graphics.DrawString?
    ... but meanwhile I'm trying to figure out why the simplest form of MeasureString didn't already work that way. ... transform done on the graphics being used the size of the string depends on the location. ... What kind of transform is being applied by default, and/or what is the default starting point used by the simplest version of MeasureString? ... A few days ago I settled on a workaround of just adding 4 to the width returned by MeasureString, so the entire legend appears in both XP and Vista. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Graphics.MeasureString? or Graphics.DrawString?
    ... With your suggested changes to the MeasureString call, in Vista it now loses the last 2 characters of each legend. ... The simplest version of Measure String only lost the last 1 character of each legend, ... Rebooting to XP on the same machine, with your suggested changes to the MeasureString call, it again loses the last 2 characters of each legend. ... the following code figures out where to position all except the last character of the string and draws all except the last character of the string. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Fixed-Width fonts
    ... > Does Windows have a fixed-width font that I can use? ... I'm calculating my character columns by the width of 1 character ... > but characters in a string aren't lining up due to what I mentioned above. ... "The MeasureString method is designed for use with individual strings ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Graphics.MeasureString? or Graphics.DrawString?
    ... With your suggested changes to the MeasureString call, in Vista it now loses the last 2 characters of each legend. ... The simplest version of Measure String only lost the last 1 character of each legend, ... Rebooting to XP on the same machine, with your suggested changes to the MeasureString call, it again loses the last 2 characters of each legend. ... the following code figures out where to position all except the last character of the string and draws all except the last character of the string. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Drawing an image at the end of a string
    ... route. ... Now the measurestring will tell me the height/width of the entire ... you need to calculate each character if not fixed length ... order to calculate a whole string. ...
    (microsoft.public.dotnet.framework.compactframework)