无法使用 TextRenderer 获得正确的 DrawnText 大小
Unable to get correct Size of DrawnText using TextRenderer
我正在使用以下代码在位图上绘制文本
GraphicsPath pth = new GraphicsPath();
var style = (int)myfont.Style;
pth.AddString(tcaption.Text, myfont.FontFamily, style, myfont.Size, point, StringFormat.GenericTypographic);
p = new Pen(new SolidBrush(bc), 2f);
mygraphics.DrawPath(p, pth);
我正在使用 TextRenderer
来测量字符串的大小..
int Width = TextRenderer.MeasureText(tcaption.Text, myfont).Width;
但这并不能产生正确大小的绘制字符串;与绘制的字符串的实际大小有大约 20-30% 的差异?
我做错了什么?请指教
更新:
我想在位图上绘制一个文本和一个图像,所以为了兼顾两者,我正在创建这样的位图
intWidth = TextRenderer.MeasureText(tcaption.Text, cfont).Width + image.Width;
intHeight = TextRenderer.MeasureText(tcaption.Text, cfont).Height +image.Height;
tempimage= new Bitmap(intWidth, intHeight);
然后我像这样从 Bitmap 创建 Graphics 对象
using (Graphics newg = Graphics.FromImage(tempimage))
@Hans Passant
我也尝试过 Graphics.MeasureString
作为 TextRenderer
的替代品
现在我设置文本和图像的位置-我需要在左上角绘制图像..所以
imageposy = 0;
imageposx = 10;
textposy = image.Height;
textposx = 0;
然后我把文字画成这样
po=new Point(textposx, textposy);
newg.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath pth = new GraphicsPath();
var style = (int)myfont.Style;
pth.AddString(tcaption.Text, myfont.FontFamily, style, myfont.Size, po,
StringFormat.GenericTypographic);
newg.FillPath(new SolidBrush(fc), pth);
现在画成这样
Rectangle nrect = new Rectangle(imageposx, imageposy, image.Width,
image.Height);
objGraphics = Graphics.FromImage(tempimage);
objGraphics.DrawImage(image, nrect);
如您所见,我需要将偏移量 10
添加到 imageposition x 坐标以更正测量问题。
希望我的更新能更清楚地说明这个问题...我做错了什么?
请指教..
而不是使用 TextRenderer 使用 GraphicsPath:
var path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, size, new Point(0, 0), StringFormat.GenericTypographic);
var area = Rectangle.Round(path.GetBounds());
这是生成具有文本大小的图像的示例代码:
private Image DrawText(String text, Font font, int size, Color textColor, Color backColor)
{
var path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, size, new Point(0, 0), StringFormat.GenericTypographic);
var area = Rectangle.Round(path.GetBounds());
Rectangle br = Rectangle.Round(path.GetBounds());
var img = new Bitmap(br.Width, br.Height);
var drawing = Graphics.FromImage(img);
drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
drawing.SmoothingMode = SmoothingMode.HighSpeed;
drawing.Clear(backColor);
drawing.TranslateTransform((img.Width - br.Width) / 2 - br.X, (img.Height - br.Height) / 2 - br.Y);
drawing.FillPath(Brushes.Black, path);
Brush textBrush = new SolidBrush(textColor);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
}
以下是示例结果:
我正在使用以下代码在位图上绘制文本
GraphicsPath pth = new GraphicsPath();
var style = (int)myfont.Style;
pth.AddString(tcaption.Text, myfont.FontFamily, style, myfont.Size, point, StringFormat.GenericTypographic);
p = new Pen(new SolidBrush(bc), 2f);
mygraphics.DrawPath(p, pth);
我正在使用 TextRenderer
来测量字符串的大小..
int Width = TextRenderer.MeasureText(tcaption.Text, myfont).Width;
但这并不能产生正确大小的绘制字符串;与绘制的字符串的实际大小有大约 20-30% 的差异?
我做错了什么?请指教
更新:
我想在位图上绘制一个文本和一个图像,所以为了兼顾两者,我正在创建这样的位图
intWidth = TextRenderer.MeasureText(tcaption.Text, cfont).Width + image.Width;
intHeight = TextRenderer.MeasureText(tcaption.Text, cfont).Height +image.Height;
tempimage= new Bitmap(intWidth, intHeight);
然后我像这样从 Bitmap 创建 Graphics 对象
using (Graphics newg = Graphics.FromImage(tempimage))
@Hans Passant
我也尝试过 Graphics.MeasureString
作为 TextRenderer
现在我设置文本和图像的位置-我需要在左上角绘制图像..所以
imageposy = 0;
imageposx = 10;
textposy = image.Height;
textposx = 0;
然后我把文字画成这样
po=new Point(textposx, textposy);
newg.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath pth = new GraphicsPath();
var style = (int)myfont.Style;
pth.AddString(tcaption.Text, myfont.FontFamily, style, myfont.Size, po,
StringFormat.GenericTypographic);
newg.FillPath(new SolidBrush(fc), pth);
现在画成这样
Rectangle nrect = new Rectangle(imageposx, imageposy, image.Width,
image.Height);
objGraphics = Graphics.FromImage(tempimage);
objGraphics.DrawImage(image, nrect);
如您所见,我需要将偏移量 10
添加到 imageposition x 坐标以更正测量问题。
希望我的更新能更清楚地说明这个问题...我做错了什么? 请指教..
而不是使用 TextRenderer 使用 GraphicsPath:
var path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, size, new Point(0, 0), StringFormat.GenericTypographic);
var area = Rectangle.Round(path.GetBounds());
这是生成具有文本大小的图像的示例代码:
private Image DrawText(String text, Font font, int size, Color textColor, Color backColor)
{
var path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, size, new Point(0, 0), StringFormat.GenericTypographic);
var area = Rectangle.Round(path.GetBounds());
Rectangle br = Rectangle.Round(path.GetBounds());
var img = new Bitmap(br.Width, br.Height);
var drawing = Graphics.FromImage(img);
drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
drawing.SmoothingMode = SmoothingMode.HighSpeed;
drawing.Clear(backColor);
drawing.TranslateTransform((img.Width - br.Width) / 2 - br.X, (img.Height - br.Height) / 2 - br.Y);
drawing.FillPath(Brushes.Black, path);
Brush textBrush = new SolidBrush(textColor);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
}
以下是示例结果: