Tuesday, November 20, 2007

To get the number of pixel taken by a string using Graphics in C#

Bitmap myImage;
Graphics g;
myImage = new Bitmap(760, 800, PixelFormat.Format32bppRgb);
g = Graphics.FromImage(myImage);


SizeF size = g.MeasureString("shekhar", new Font("Arial", 8));
int lenght = (int)size.Width;
int height1 = (int)size.Height;

this will give the length and height in pixel of the string "shekhar"