Drawstring画出来的文本的边距问题

CraxyMouse 2010-03-11 06:36:08
用drawstring画出来有空白的地方,应该如何缩小空白?
或是其它的解决方法



代码片段:
float fheight = g.MeasureString(Text, TextFont.ShowFont).Height;
float fwidth = GetTextSize(text, TextFont.ShowFont,"W")*_pZoom;

TextRectangle = Rectangle.Round(new RectangleF(TextRectangle.X, TextRectangle.Y, fwidth, fheight));
g.DrawString(text, TextFont.ShowFont, sb,TextRectangle.X,TextRectangle.Y);

以上代码画出来的就是上图文本与虚线框有空白。
有什么方法可以缩小空白?
谢谢大家!
...全文
378 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
CraxyMouse 2010-03-12
  • 打赏
  • 举报
回复
还是不对
jingxin1517 2010-03-12
  • 打赏
  • 举报
回复
别费力气了,GDI+做不到的。如果你的字体全都是相同的,可以试试,先将文本写在图片Image1(图片大小是你的矩形区域大小,即你写的fwidth, fheight)上,然后再定义一张图片Image2(大小还是是你图中的矩形区域),将Image1的指定区域写在Image2的指定区域上。
Bitmap Image2= new Bitmap(fwidth,fheight);
Graphics g = Graphics.FromImage(Image2);
g.DrawImage(Image1, 0, 0, New Rectangle(10, 10, Image1.Width-10, Image1.Height-10), GraphicsUnit.Pixel);//将图片Image1的(10,10)到(Image1.Width-10,Image1.Height-10)区域,也就是文本区域,写在Image2的从(0,0)开始的区域。10就是你的文本到虚线的区域,这个10要自己测试修正,看值多大合适
lzsh0622 2010-03-12
  • 打赏
  • 举报
回复
试试下例中的用法:
TextBoxRenderer 类
lzsh0622 2010-03-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 dunao 的回复:]
不知道用矢量图做行不行!
[/Quote]

你贴的图不是用的矢量图?

如果类似画图板,只是看着漂亮,实际上没有什么使用价值。

最好用矢量图做,可以修改,可以缩放。
CraxyMouse 2010-03-12
  • 打赏
  • 举报
回复
不知道用矢量图做行不行!
wuyq11 2010-03-11
  • 打赏
  • 举报
回复
FontStyle fs = 0;
Font a = new Font(this.SelectedFontFamily, 150, fs);
Bitmap bmp = new Bitmap(300,300);
Brush b=Brushes.Black;
Graphics g = Graphics.FromImage(bmp);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.DrawString("", a, b, new PointF(10, 10));
g.Dispose();
bmp.Save("D:\\a.png");
MessageBox.Show("OK");
Optione 2010-03-11
  • 打赏
  • 举报
回复
//为什么要这样呢?
TextRectangle = Rectangle.Round(new RectangleF(TextRectangle.X, TextRectangle.Y, fwidth, fheight));
//直接
RectangleF TextRectangle = new RectangleF(TextRectangle.X, TextRectangle.Y, fwidth, fheight);
CraxyMouse 2010-03-11
  • 打赏
  • 举报
回复
这个应该不对

[Quote=引用 1 楼 mngzilin 的回复:]
_pZoom值是多少?

参考:
string str="tesdsdasfsat";
Font f= new Font(FontFamily.GenericSansSerif,5,FontStyle.Italic);
SizeF s = g.MeasureString(str, f);
g.DrawString(str,f, Brushes.Tomato,width-s.Width,height-s.Height);

[/Quote]
xiaogug01 2010-03-11
  • 打赏
  • 举报
回复
这个可不懂 。。。。帮顶了。。。。。期待学习中。。。。
CraxyMouse 2010-03-11
  • 打赏
  • 举报
回复
pZoom 的值为1
mngzilin 2010-03-11
  • 打赏
  • 举报
回复
_pZoom值是多少?

参考:
string str="tesdsdasfsat";
Font f= new Font(FontFamily.GenericSansSerif,5,FontStyle.Italic);
SizeF s = g.MeasureString(str, f);
g.DrawString(str,f, Brushes.Tomato,width-s.Width,height-s.Height);

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧