By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,909 Members | 2,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,909 IT Pros & Developers. It's quick & easy.

point to pixel conversion

P: n/a
I have an html table that I am filling with data from the database. when I
put the text in the table, I only display the number of characters that will
fit based on the size of the cell.

I know the width of the cell, I can get the length of the text, but the
cell.width is in pixels, while the text length is measured based on points
(font size).

anyone have any ideas how to get this to work? Here is the entire class I
am using.
Thank you in advance

Kirk
-------------------
Public Class TextCalc
Public Shared f As Drawing.Font
Public Shared g As Drawing.Graphics
Public Shared Ready As Boolean = False

Friend Shared Sub SetUpCalculator(ByVal FontFamily As String, ByVal
FontUnit As Web.UI.WebControls.Unit)
f = New Drawing.Font(FontFamily, Convert.ToSingle(FontUnit.Value),
CType(FontUnit.Type, Drawing.GraphicsUnit))

g = Drawing.Graphics.FromImage(New Drawing.Bitmap(100, 100))
Ready = True
End Sub
Friend Shared Function GetDisplayText(ByVal ItemText As String, ByVal
cellWidth As Single) As String
Dim l As Drawing.SizeF
Dim x As Int16
Dim s As String

l = g.MeasureString(ItemText, f)
If l.Width <= cellWidth Then ' this is the line that causes the
problem,
'l.Width is based on
points, cellWidth is based on pixels
Return ItemText
End If

For x = 1 To ItemText.Length
s = ItemText.Substring(0, x) & ".."

l = g.MeasureString(s, f)

If l.Width >= cellWidth Then
Exit For
End If
Next
Return s
End Function
End Class
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
this approach will not work, because you are calculating the pixels based on
the server's font size, not the client's. only the client knows the actual
pixels per point. this is because the client may actually use a different
font or size then the server.

this is also why grid layout does not work in the general case (switch to
large fonts and see result).

you can use a div in the table cell and have it truncate the data by
specifying the overflow style.

-- bruce (sqlwork.com)

"Kirk Graves" <kr***********@yahoo.com> wrote in message
news:ex**************@TK2MSFTNGP10.phx.gbl...
I have an html table that I am filling with data from the database. when I put the text in the table, I only display the number of characters that will fit based on the size of the cell.

I know the width of the cell, I can get the length of the text, but the
cell.width is in pixels, while the text length is measured based on points
(font size).

anyone have any ideas how to get this to work? Here is the entire class I
am using.
Thank you in advance

Kirk
-------------------
Public Class TextCalc
Public Shared f As Drawing.Font
Public Shared g As Drawing.Graphics
Public Shared Ready As Boolean = False

Friend Shared Sub SetUpCalculator(ByVal FontFamily As String, ByVal
FontUnit As Web.UI.WebControls.Unit)
f = New Drawing.Font(FontFamily, Convert.ToSingle(FontUnit.Value),
CType(FontUnit.Type, Drawing.GraphicsUnit))

g = Drawing.Graphics.FromImage(New Drawing.Bitmap(100, 100))
Ready = True
End Sub
Friend Shared Function GetDisplayText(ByVal ItemText As String, ByVal
cellWidth As Single) As String
Dim l As Drawing.SizeF
Dim x As Int16
Dim s As String

l = g.MeasureString(ItemText, f)
If l.Width <= cellWidth Then ' this is the line that causes the
problem,
'l.Width is based on points, cellWidth is based on pixels
Return ItemText
End If

For x = 1 To ItemText.Length
s = ItemText.Substring(0, x) & ".."

l = g.MeasureString(s, f)

If l.Width >= cellWidth Then
Exit For
End If
Next
Return s
End Function
End Class

Nov 18 '05 #2

P: n/a
thanks bruce.

I will try it.

"bruce barker" <no***********@safeco.com> wrote in message
news:uJ**************@TK2MSFTNGP10.phx.gbl...
this approach will not work, because you are calculating the pixels based on the server's font size, not the client's. only the client knows the actual
pixels per point. this is because the client may actually use a different
font or size then the server.

this is also why grid layout does not work in the general case (switch to
large fonts and see result).

you can use a div in the table cell and have it truncate the data by
specifying the overflow style.

-- bruce (sqlwork.com)

"Kirk Graves" <kr***********@yahoo.com> wrote in message
news:ex**************@TK2MSFTNGP10.phx.gbl...
I have an html table that I am filling with data from the database. when
I
put the text in the table, I only display the number of characters that will
fit based on the size of the cell.

I know the width of the cell, I can get the length of the text, but the
cell.width is in pixels, while the text length is measured based on

points (font size).

anyone have any ideas how to get this to work? Here is the entire class I am using.
Thank you in advance

Kirk
-------------------
Public Class TextCalc
Public Shared f As Drawing.Font
Public Shared g As Drawing.Graphics
Public Shared Ready As Boolean = False

Friend Shared Sub SetUpCalculator(ByVal FontFamily As String, ByVal
FontUnit As Web.UI.WebControls.Unit)
f = New Drawing.Font(FontFamily, Convert.ToSingle(FontUnit.Value), CType(FontUnit.Type, Drawing.GraphicsUnit))

g = Drawing.Graphics.FromImage(New Drawing.Bitmap(100, 100))
Ready = True
End Sub
Friend Shared Function GetDisplayText(ByVal ItemText As String, ByVal cellWidth As Single) As String
Dim l As Drawing.SizeF
Dim x As Int16
Dim s As String

l = g.MeasureString(ItemText, f)
If l.Width <= cellWidth Then ' this is the line that causes the problem,
'l.Width is

based on
points, cellWidth is based on pixels
Return ItemText
End If

For x = 1 To ItemText.Length
s = ItemText.Substring(0, x) & ".."

l = g.MeasureString(s, f)

If l.Width >= cellWidth Then
Exit For
End If
Next
Return s
End Function
End Class


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.