I assume the length of string is 25, and you want to set the 25 as the value of div's width/height. If I don't misunderstand your requirement please check my sample code:
Hi,
Assume that text length is 25, I want to convert this to pixel size, pixel size is different from text length.
I assume the length of string is 25, and you want to set the 25 as the value of div's width/height
Sorry for this mistake. Actually, my sample doesn't use the length of string as the value of width of div. I use the Graphics.MeasureString method to measure the inputed string and then get the pixel.Width to set the width of div:
//gets maximum length of string in collection
string maxString = txtInput.Text;
//converts string length to pixel
System.Drawing.SizeF sizeF = new System.Drawing.SizeF();
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new System.Drawing.Bitmap(1, 1));
sizeF = graphics.MeasureString(maxString, new System.Drawing.Font("Arial,Verdana,Helvetica,sans-serif", 10,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point));
//Font stringFont = new Font("Arial", 16);
//sizeF = graphics.MeasureString(maxString, stringFont);
int maxTextWidthinPixel = Convert.ToInt32(sizeF.Width);
int maxTextHeightinPixel = Convert.ToInt32(sizeF.Height);
div1.Style.Add("width", maxTextWidthinPixel.ToString());
div1.Style.Add("border-style", "solid");
div1.Style.Add("border-color", "Red");
div1.InnerText = maxString;
//div1.Style.Add("height", maxTextHeightinPixel.ToString());
Response.Write("Length of input string:" + maxString.Length);
Response.Write("</br>");
Response.Write("Width of pixel:"+maxTextWidthinPixel.ToString());
Response.Write("<br/>");
Response.Write("Height of Pixel:"+maxTextHeightinPixel.ToString());
Please mark the replies as answers if they help or unmark if not.
Feedback to us
I assume the length of string is 25, and you want to set the 25 as the value of div's width/height. If I don't misunderstand your requirement please check my sample code:
Hi,
Assume that my string length is 25 & i want to convert it to pixel, pixel length is always greater than the string length when tested in developer tools. so i need to know how to convert from length to pixel.
I assume the length of string is 25, and you want to set the 25 as the value of div's width/height. If I don't misunderstand your requirement please check my sample code:
Hi,
Using the same string length as pixel is not my requirement. I need to convert the string length to pixel, pixel size is different from string length.
I assume the length of string is 25, and you want to set the 25 as the value of div's width/height. If I don't misunderstand your requirement please check my sample code:
Hi,
My requirement is to convert the string length to pixel size. pixel size is from string length always.
pixel length is always greater than the string length when tested in developer tools.
Yes. Could you run my sample code? After useing the MeasureString method, I get the SizeF and and then set the SizeF.With to div's width. If I use the string "FDASFfdasfas", I got this displayed on my web page:
Length of input string:12
Width of pixel:94
Height of Pixel:17
Please mark the replies as answers if they help or unmark if not.
Feedback to us
When tested in browser, actual width: 88px is enough to fit for the text. I don't know why this extra size is coming in conversion?
The conversin is correct exactly. To apply the pixel to browser. I guess some compatible issue may occur. That doesn't mean the conversion is wrong. The 88px is enough, but it may not be best, instead 94 may be. Just my consideration.
Please mark the replies as answers if they help or unmark if not.
Feedback to us
I'm able to convert the string length to pixel, but the converted size is always exceeds above 20px than the actual size when tested in developer tools. Below is the code that i used for reference.
I resolved my issue by changing that GraphicsUnit & FontStyle.
sizeF = graphics.MeasureString(largestString, new Font(fontNames, fontSize, FontStyle.Bold, GraphicsUnit.Pixel));
Anandhan.S
Marked as answer by anandhan on May 02, 2012 11:07 AM
anandhan
Member
1 Points
44 Posts
Re: String length to pixel conversion
Apr 26, 2012 06:26 PM|LINK
Hi,
Assume that text length is 25, I want to convert this to pixel size, pixel size is different from text length.
Mamba Dai - ...
All-Star
23531 Points
2683 Posts
Microsoft
Re: String length to pixel conversion
Apr 27, 2012 03:42 AM|LINK
Sorry for this mistake. Actually, my sample doesn't use the length of string as the value of width of div. I use the Graphics.MeasureString method to measure the inputed string and then get the pixel.Width to set the width of div:
//gets maximum length of string in collection string maxString = txtInput.Text; //converts string length to pixel System.Drawing.SizeF sizeF = new System.Drawing.SizeF(); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new System.Drawing.Bitmap(1, 1)); sizeF = graphics.MeasureString(maxString, new System.Drawing.Font("Arial,Verdana,Helvetica,sans-serif", 10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)); //Font stringFont = new Font("Arial", 16); //sizeF = graphics.MeasureString(maxString, stringFont); int maxTextWidthinPixel = Convert.ToInt32(sizeF.Width); int maxTextHeightinPixel = Convert.ToInt32(sizeF.Height); div1.Style.Add("width", maxTextWidthinPixel.ToString()); div1.Style.Add("border-style", "solid"); div1.Style.Add("border-color", "Red"); div1.InnerText = maxString; //div1.Style.Add("height", maxTextHeightinPixel.ToString()); Response.Write("Length of input string:" + maxString.Length); Response.Write("</br>"); Response.Write("Width of pixel:"+maxTextWidthinPixel.ToString()); Response.Write("<br/>"); Response.Write("Height of Pixel:"+maxTextHeightinPixel.ToString());Feedback to us
Develop and promote your apps in Windows Store
anandhan
Member
1 Points
44 Posts
Re: String length to pixel conversion
Apr 29, 2012 06:20 AM|LINK
Hi,
Assume that my string length is 25 & i want to convert it to pixel, pixel length is always greater than the string length when tested in developer tools. so i need to know how to convert from length to pixel.
anandhan
Member
1 Points
44 Posts
Re: String length to pixel conversion
Apr 29, 2012 04:24 PM|LINK
Hi,
Using the same string length as pixel is not my requirement. I need to convert the string length to pixel, pixel size is different from string length.
anandhan
Member
1 Points
44 Posts
Re: String length to pixel conversion
Apr 29, 2012 04:28 PM|LINK
Hi,
My requirement is to convert the string length to pixel size. pixel size is from string length always.
Mamba Dai - ...
All-Star
23531 Points
2683 Posts
Microsoft
Re: String length to pixel conversion
Apr 30, 2012 06:37 AM|LINK
Yes. Could you run my sample code? After useing the MeasureString method, I get the SizeF and and then set the SizeF.With to div's width. If I use the string "FDASFfdasfas", I got this displayed on my web page:
Length of input string:12
Width of pixel:94
Height of Pixel:17
Feedback to us
Develop and promote your apps in Windows Store
anandhan
Member
1 Points
44 Posts
Re: String length to pixel conversion
May 01, 2012 05:11 PM|LINK
Yes, I'm getting the output, but the size is not exactly matches in browser,
For example: String Length: 12, Width in Pixel:94
When tested in browser, actual width: 88px is enough to fit for the text. I don't know why this extra size is coming in conversion?
Mamba Dai - ...
All-Star
23531 Points
2683 Posts
Microsoft
Re: String length to pixel conversion
May 02, 2012 02:45 AM|LINK
The conversin is correct exactly. To apply the pixel to browser. I guess some compatible issue may occur. That doesn't mean the conversion is wrong. The 88px is enough, but it may not be best, instead 94 may be. Just my consideration.
Feedback to us
Develop and promote your apps in Windows Store
anandhan
Member
1 Points
44 Posts
Re: String length to pixel conversion
May 02, 2012 11:07 AM|LINK
I resolved my issue by changing that GraphicsUnit & FontStyle.
sizeF = graphics.MeasureString(largestString, new Font(fontNames, fontSize, FontStyle.Bold, GraphicsUnit.Pixel));