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.
//gets maximum length of string in collection
string maxString = ListItems.OrderByDescending(m => m.Length).First();
//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));
int maxTextWidthinPixel = Convert.ToInt32(sizeF.Width);
I test your code and Unit.Pixel, they both work fine. My tested code is the same as yours, I don't know why they couldn't work fine on your development environment. I suggest you to re-create a simple appliction to test my sample code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//gets maximum length of string in collection
string maxString = "gfdgasdf fdafafadfasfasdfdsfas";
//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));
int maxTextWidthinPixel = Convert.ToInt32(sizeF.Width);
int maxTextHeightinPixel = Convert.ToInt32(sizeF.Height);
Response.Write(maxTextWidthinPixel.ToString());
Response.Write("<br/>");
Response.Write(maxTextHeightinPixel.ToString());
Response.Write("<br/>");
Response.Write(Unit.Pixel(23).Value.ToString());
Response.Write("<br/>");
Response.Write(Unit.Pixel(23).ToString());
}
}
Please mark the replies as answers if they help or unmark if not.
Feedback to us
My requirement is to convert the string length to pixel & assign it to div dynamically.
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:
anandhan
Member
1 Points
44 Posts
String length to pixel conversion
Apr 17, 2012 10:07 AM|LINK
Hi,
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.
//gets maximum length of string in collection string maxString = ListItems.OrderByDescending(m => m.Length).First(); //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)); int maxTextWidthinPixel = Convert.ToInt32(sizeF.Width);How to correct my error in the code?
thomas.watso...
Member
182 Points
51 Posts
Re: String length to pixel conversion
Apr 17, 2012 10:09 AM|LINK
if so why are you not just subtract 20 from resulted value?
Srikanth Kas...
Contributor
4299 Points
883 Posts
Re: String length to pixel conversion
Apr 17, 2012 10:10 AM|LINK
Use like this :
Srikanth Kasturi
Please "Mark As Answer" if my post serves purpose.
anandhan
Member
1 Points
44 Posts
Re: String length to pixel conversion
Apr 17, 2012 10:22 AM|LINK
20 is not a contant always... it varies based on the string length..
anandhan
Member
1 Points
44 Posts
Re: String length to pixel conversion
Apr 17, 2012 10:25 AM|LINK
Hi,
The below code always displays the string lenth as unit. e.g. string lengh = 23, Unit is also 20, This is not converted to pixel unit.
Mamba Dai - ...
All-Star
23530 Points
2683 Posts
Microsoft
Re: String length to pixel conversion
Apr 19, 2012 04:53 PM|LINK
Hi,
I test your code and Unit.Pixel, they both work fine. My tested code is the same as yours, I don't know why they couldn't work fine on your development environment. I suggest you to re-create a simple appliction to test my sample code:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //gets maximum length of string in collection string maxString = "gfdgasdf fdafafadfasfasdfdsfas"; //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)); int maxTextWidthinPixel = Convert.ToInt32(sizeF.Width); int maxTextHeightinPixel = Convert.ToInt32(sizeF.Height); Response.Write(maxTextWidthinPixel.ToString()); Response.Write("<br/>"); Response.Write(maxTextHeightinPixel.ToString()); Response.Write("<br/>"); Response.Write(Unit.Pixel(23).Value.ToString()); Response.Write("<br/>"); Response.Write(Unit.Pixel(23).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 20, 2012 11:59 AM|LINK
Hi,
I tested your sample code & observed that Unit.Pixel is doing nothing. It's returning the string length as it is.
Mamba Dai - ...
All-Star
23530 Points
2683 Posts
Microsoft
Re: String length to pixel conversion
Apr 23, 2012 05:30 AM|LINK
Maybe this is relative to your developer tools, not these souce code above. Could you explain what your requirement is exactly?
Feedback to us
Develop and promote your apps in Windows Store
anandhan
Member
1 Points
44 Posts
Re: String length to pixel conversion
Apr 23, 2012 09:22 AM|LINK
My requirement is to convert the string length to pixel & assign it to div dynamically.
Mamba Dai - ...
All-Star
23530 Points
2683 Posts
Microsoft
Re: String length to pixel conversion
Apr 24, 2012 07:36 AM|LINK
Hi,
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:
Aspx file:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtInput" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <div runat="server" id="div1"> </div> </div> </form> </body> </html>Code behind file:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.Drawing; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //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)); 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(maxTextWidthinPixel.ToString()); Response.Write("<br/>"); Response.Write(maxTextHeightinPixel.ToString()); } }Feedback to us
Develop and promote your apps in Windows Store