Pixel Width of Char in Web App

  • Thread starter Thread starter Sangeeta
  • Start date Start date
S

Sangeeta

Hi,

I have a datagrid where columns are of some fixed pixel size. If any
column text increases the width of column, the plan is to truncate it
till its pixel width becomes less than the coloumn width.

Is there a way to find the size of a char in pixel given the font name
and size. I have tried using System.Drawing.Graphics object, but the
width attribute of the returned fSize object does not return value in
pixels. I am including the code in this mail, can anyone tell me if I
there is another approach or if I am doing this the wrong way.

Thanks,
Sangeeta

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Font ft= new Font("CourierThai", 14);
System.Drawing.Image img = System.Drawing.Image.FromFile
(MapPath
"Images/Deepa.jpg"));
System.Drawing.Graphics gh = System.Drawing.Graphics.FromImage(img);
gh.PageUnit = GraphicsUnit.Pixel;
Response.Write(gh.DpiX);
Response.Write(gh.MeasureString("Test", ft).Width);
Response.Write(ft.SizeInPoints);

}
 
You have absolutely no control over what font and at which size the user
will see, so forget about being to able to measure that. If you want to
truncate the overflowing text just use styles, overflow: hidden is the one
you want.

Jerry
 
Try the <col> elements in your table (I don't think IE parses overflow on
TDs). Like this:

<div ...> <!-- This is your parent div with overflow: scroll -->
<table>
<col style="overflow: hidden;" span="...">
<tbody>
....
</tbody>
</table>

set the span to how many columns your table has, or just repeat the col that
many times or use styles (which I'd prefer).

Jerry
 
Oh and if you set your table-layout style to fixed the clients should use
fixed column widths and just hide the overflow.
 
Back
Top