Re: Graphics.MeasureString too short



MeasureString has some usable overloads that may help you. For example, I use:

SizeF tabSize = graphics.MeasureString( "M", font, rectfText.Size, StringFormat.GenericTypographic );

This overload is described in http://msdn.microsoft.com/en-us/library/ms142109.aspx
The note under Remarks is of interest:

The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the
string to allow for overhanging glyphs. Also, the DrawString method adjusts glyph points to optimize display quality and might
display a string narrower than reported by MeasureString. To obtain metrics suitable for adjacent strings in layout (for example,
when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a
StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.

Hope this helps,
Mike

"Rune Huseby" <rune@xxxxxxxxx> wrote in message news:Xns9B32A42732476runehusebynewschello@xxxxxxxxxxxxxxxx
"Rainer Queck" <Rainer@xxxxxxxxxxxxxxx> wrote in
news:u9l9Z7eKJHA.5328@xxxxxxxxxxxxxxxxxxxx:

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 short string width.
What is it I am doing wrong?

Here the code I am using:
private void MinimizeDgvColumns(DataGridView dgv)
{
using (Graphics g = dgv.CreateGraphics())
{
foreach (DataGridViewColumn c in dgv.Columns)
{
string s = c.HeaderCell.Value.ToString();
c.Width = (int)graphics.MeasureString(s,
dgv.ColumnHeadersDefaultCellStyle.Font).Width;
}
}
}

You need to have space for the gridlines to, and some margin.

This is how the text-box-columns calculate its preferred size:
protected internal override Size GetPreferredSize(Graphics g, object value)
{
Size extents = Size.Ceiling(g.MeasureString(GetText(value),
DataGridTableStyle.DataGrid.Font));
extents.Width += xMargin*2 + this.DataGridTableStyle.GridLineWidth;
extents.Height += yMargin;
return extents;
}


hint: The .NET source-code can be downloaded using this tool:
http://www.codeplex.com/NetMassDownloader

--
Rune Huseby


.



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: printing on the right side of paper
    ... and print the string as it provided better measurement for this. ... default StringFormat used in MeasureString some padding around the string ... You will also need to consider printer hard margins when printing as 0,0 ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Graphics.MeasureString? or Graphics.DrawString?
    ... It looks like MeasureString is pretty much useless in Vista. ... Measure String only lost the last 1 character of each legend, ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: [STRING] retrieve the pixel length, MeasureString
    ... You need an instance of a Graphics object. ... But it seems that the MeasureString method is not available on Asp.Net. ... However, it's not very useful in an ASP.NET application, as you are measuring the string for the screen on the server, and it's displayed in the browser on the client. ...
    (microsoft.public.dotnet.languages.vb)
  • MeasureString - Slighly narrower than actual text.
    ... When I use the MeasureString method of the [Graphics] object to determine ... Size that has a width slightly narrower than the actual string. ... At the moment I'm increasing the width by the point-size of the font, ...
    (microsoft.public.dotnet.framework.drawing)