How do you get the size of a string? In Windows Forms it's easy, I just use graphics object and then MeasureString function. In ASP.NET I'm not sure how to do this.
Like Tom Gullen is saying. You can just create a bitmap and messure the string. I have this code I use to find the width/length in pixel. Just change the font and size.
// Bitmap class namespace:
using System.Drawing;
...
private float GetWidthOfString(string str)
{
Bitmap objBitmap = default(Bitmap);
Graphics objGraphics = default(Graphics);
objBitmap = new Bitmap(500, 200);
objGraphics = Graphics.FromImage(objBitmap);
SizeF stringSize = objGraphics.MeasureString(str, new Font("Arial", 12));
objBitmap.Dispose();
objGraphics.Dispose();
return stringSize.Width;
}
Just wanted to show an example.
String.Length
Returns the length of the string (number of characters).
C#
string MyString = "Stack Overflow String";
Response.Write(MyString.Length);
VB.net
Dim MyString As String = "Stack Overflow String"
Response.Write(MyString.Length);
MSDN Reference
http://msdn.microsoft.com/en-us/library/system.string.length.aspx
Physical String Size Serverside
I'm not sure why so many people are apparently determined to put forward the idea that it is 'flat out impossible' to do this server side, it isn't. You just need to be a little bit creative, and be willing to do a lot of testing.
There is no native function I know of to measure the pixel dimensions of a string. You can achieve it in the following ways. Note you need to know:
- The font family
- Font size
- Container dimensions (for when the text wraps lines)
- Any other CSS rules such as word spacing are going to make the calculation a lot less accurate and more complex. Try and avoid it if accuracy is important.
1. Render it to an image and measure image size
Draw the text to a bitmap, and then measure it. This should be reasonably exact.
2. Use a fixed width font
If you are using a fixed width font and the text doesn't wrap, you can simply multiply its width by the number of characters.
3. Rough estimation
You could get a reasonably rough estimation on non fixed width fonts by picking some arbitrary character width number and multiplying it by total characters. Depends how accurate you want to be.
If you are using JQuery, see this answer
Determine Pixel Length of String in Javascript/jQuery?
I'm sure if not you can work something similar.
The following method will provide the size of a String rendered in a given font:
private RectangleF sizeOfString(string text, Font font)
{
GraphicsPath path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, font.Size, new Point(0, 0), new StringFormat());
return path.GetBounds();
}
You could then use this to get the width as follows
float width = sizeOfString("Hello World", someFont).Width;
You can use the Length property on a string as follows:
It is a completely impossible thing to do on server side (assuming by “ASP.NET”, you mean web pages created using ASP.NET”). ASP.NET outputs HTML, which is a markup language interpreted and rendered by a browser on the client. The same HTML code can be displayed on various devices, in various browsers, using various installed fonts, etc., resulting in grossly differing display.
Your ASP.NET site, running code on the server, does not know which font, what resolution, etc. your client uses. The only theoretical way this could be done would be to run client-side code (e.g. JavaScript, Silverlight, Flash, …) to report back to the server. However, this would be quite difficult.
Generally, you do not need to do that, you do not want to do that.
@Tom Gullen, @asawyer
This is my temporary solution and it seems to be working for now. fyi, my project happened to be a asp.net application with many dynamic interfaces involved in it (thats another story). At anyrate my short term solution is posted below. Im acually using a modalpopupextender (with listbox) on the fly when the user clicks a textbox (custom combobox). The ajax combobox doesn't work very well especially when there is many of them and you can't set the dropdown width on a dropdownlist.
Friend Function ResizePopup(ByVal sender As Object, ByVal standardwidth As Integer) As Integer
Dim lst As ListBox = CType(sender, ListBox)
Dim itemlength As Single = 0
'get longest string length
For Each item As ListItem In lst.Items
If item.Text.Length > itemlength Then
itemlength = item.Text.Length
End If
Next
'set a general multiplier
Dim newWidth As Integer = itemlength * 7
'compare to width of control, if larger, then use value else return control width
If newWidth > standardwidth Then
Return newWidth
Else
Return standardwidth
End If
End Function
@Eystein Bye's solution, made slightly cleaner with usings:
using (Bitmap objBitmap = new Bitmap(5000, 200))
{
using (Graphics objGraphics = Graphics.FromImage(objBitmap))
{
SizeF stringSize = objGraphics.MeasureString(txt, new Font("Arial", 12));
return stringSize.Width;
}
}
© 2022 - 2024 — McMap. All rights reserved.