计算字符串的像素长度
将要计算像素长度的字符串画在图片上面,然后再计算字符串的像素
/// <summary> /// 获取当前字符串的像素长度 ///将字符串画在图片上面计算像素 /// create by gaoxing /// </summary> /// <param name="str">输入字符串</param> /// <param name="picPath">传入图片路径</param> /// <returns>当前字符串的长度</returns> private static float GetStringLength(string str, string picPath) { // Bitmap bitmap = new Bitmap(System.Drawing.Image.FromFile(picPath)); Graphics g = Graphics.FromImage(bitmap); /////////////////////计算字符串的长度///////////////////////// Graphics tmpG = g; SizeF size = tmpG.MeasureString(str, new Font(new FontFamily("Times New Roman"), 18, FontStyle.Bold)); float StringWidth = size.Width; return StringWidth; ////////////////////////////////////////////// }