C#用Graphics书写文字时计算字符串所占的像素单位大小

System.Drawing.Graphics g = e.Graphics; // 获得一个Graphics实例
string str = "string";
System.Drawing.Font font = new System.Drawing.Font(new System.Drawing.FontFamily(this.FontFamily.Source), 32);
System.Drawing.SizeF size = g.MeasureString(str, font);  // 计算字符串所需要的大小
g.DrawString(str, font, System.Drawing.Brushes.White, new System.Drawing.PointF((1920 - size.Width) / 2, 30));



你可能感兴趣的:(C#)