I'm working on a program that allows using a bitmap (in a PictureBox) as a text object. I have things mostly working, even word-wrap, all except for outlined text.
private void DrawOutlineString(string text, PointF pos)
{
Graphics g = Graphics.FromImage(image);
SolidBrush b = new(forecolor);
Pen pen = new(outlinecolor, outlinesize);
Pen p = pen;
GraphicsPath gp = new();
gp.AddString(text, fontfamily, (int)fontstyle, g.DpiY
* fontsize / 72, pos, new StringFormat());
g.DrawPath(p, gp);
g.FillPath(b, gp);
gp.Dispose();
p.Dispose();
b.Dispose();
g.Dispose();
}
For outlined text, I'm using a GraphicsPath
object and adding the string with font info to it, then using DrawPath
and then FillPath
to draw it.
Note that DrawPath
uses a pen and the pen's thickness determines how thick the outline is.
The problem is that the size of the text put to the bitmap that way is not the same as the size it would be with a DrawString
. But it is not the same as simply adding the outline size to the g.MeasureString
result.
private SizeF GetTextSize(string text)
{
Graphics g = Graphics.FromImage(image);
SizeF size = g.MeasureString(text, font);
g.Dispose();
return size;
}
private SizeF GetOutlineTextSize(string text)
{
SizeF size = GetTextSize(text);
size.Width += outlinesize * 2;
size.Height += outlinesize * 2;
return size;
}
I've tried adding the outline size (as defined in my pen) to the g.MeasureString
size and I've tried doubling the outline size (since once on each side), but no such luck. Help!
Okay, after a cursory examination of the link provided by Jimi, here is my latest attempt:
private SizeF GetOutlineTextSize(string text)
{
Graphics g = Graphics.FromImage(image);
Pen p = new Pen(outlinecolor, outlinesize);
GraphicsPath gp = new();
gp.AddString(text, fontfamily, (int)fontstyle, g.DpiY * fontsize / 72, position, new StringFormat());
RectangleF rect = gp.GetBounds(null, p);
gp.Dispose();
p.Dispose();
g.Dispose();
SizeF size = new(rect.Width, rect.Height);
return size;
}
I haven't had a chance to fully test this method yet, but it looks hopeful.