C#测量字体大小的问题
来源:互联网 发布:mysql nvl函数用法 编辑:程序博客网 时间:2024/05/18 18:45
最近写个程序很郁闷,用Gdi+ 的MeasureString测量的宽度好高度都要比DrawString 绘制的高。所以索性采用GraphicsPath来进行绘图,后面测试的结果的是满意的,但是发现字体又偏小,终于找到了解决方案:如下
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = e.Graphics;
graphics.SmoothingMode = SmoothingMode.HighQuality;
GraphicsPath path = new GraphicsPath();
FontFamily family = new FontFamily("楷体");
Font font = new Font(family, 300, FontStyle.Bold);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Near;
string str = "8E";
float emSize = graphics.DpiY * font.Size / 72;
path.AddString(str, family, (int)font.Style, emSize, new PointF(0, 0), stringFormat);
RectangleF rect = path.GetBounds();
e.Graphics.FillPath(Brushes.Black, path);
graphics.DrawRectangle(new Pen(Color.Red), rect.X, rect.Y, rect.Width, rect.Height);
// 得到测量宽度
float width = rect.Width;
// 得到测量高度
float height = rect.Height;
// 以下测试MeasureString,测试结果是长度要长于文字
SizeF size = graphics.MeasureString(str, font, new PointF(0, 0), stringFormat);
graphics.DrawString(str, font, new SolidBrush(Color.Red),new PointF(0, height + 10),stringFormat );
//graphics.DrawEllipse(new Pen(Color.Red), new RectangleF(0,height +5, size.Width, size.Height));
graphics.DrawRectangle(new Pen(Color.Red), new Rectangle(0, (int)height + 5, (int)size.Width, (int)size.Height));
}
以上涵盖两种测绘方式的结果
- C#测量字体大小的问题
- FontMetrics字体大小测量
- 测量电阻遇到的问题
- android View的测量问题
- TextView的高度测量问题。
- 关于listview的字体大小问题!
- IOS字体大小,字号的问题
- 测量C#代码的运行时间
- 在C#中测量字符串的尺寸
- apk字体大小跟着系统字体大小变化的问题处理
- Android 字体属性及测量 确定字体大小
- 关于MeasureString测量字体的长度问题
- 微博营销的第一步测量问题
- 关于移动测量姿态问题的思考
- 土地面积测量存在问题的一些建议
- PostGIS测量面积、缓冲区单位的问题
- MC9S08DZ60单片机测量时间的问题
- 关于自定义TextView的测量问题
- Sahara集群的状态一览
- eclipse快捷键总结
- ubuntu14.04下ruby和rails的安装
- JVM的"发家"史
- 防止SQL注入
- C#测量字体大小的问题
- C#中如何获取一个字体的宽度值(像素单位)?
- hibernate中的关联映射图解----多对一
- AFN报错问题
- Intellj idea 快捷键总结
- 输入对象:FileReader小结
- JRockit检测Tomcat内存溢出JAVA内存泄漏问题
- Leetcode String to Integer (atoi)
- hibernate中get与load区别