我有以下代码。有没有一种简单的方法可以在我正在编写的文本上勾勒出轮廓?
var imageEncoder = Encoder.Quality;
var imageEncoderParameters = new EncoderParameters(1);
imageEncoderParameters.Param[0] = new EncoderParameter(imageEncoder, 100L);
var productImage = GetImageFromByteArray(myViewModel.ProductImage.DatabaseFile.FileContents);
var graphics = Graphics.FromImage(productImage);
var font = new Font("Segoe Script", 24);
var brush = Brushes.Orange;
var container = new Rectangle(myViewModel.ContainerX, myViewModel.ContainerY, myViewModel.ContainerWidth, myViewModel.ContainerHeight);
var stringFormat = new StringFormat {Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center};
graphics.DrawString(customizationText, font, brush, container, stringFormat);
最佳答案
是的。代替DrawString,请使用以下调用序列:
new GraphicsPath
(创建一个空的GraphicsPath
)GraphicsPath.AddString
(GraphicsPath
对象现在表示文本的轮廓)Graphics.DrawPath
(以所需的任何Pen
绘制轮廓)如果需要将
GraphicsPath.AddString
与Graphics.DrawString
一起使用,则需要转换字体大小,因为Graphics.DrawString
期望“点大小”,而GraphicsPath.AddString
期望“em大小”。转换公式只是emSize = g.DpiY * pointSize / 72
。这是一个代码示例:
// assuming g is the Graphics object on which you want to draw the text
GraphicsPath p = new GraphicsPath();
p.AddString(
"My Text String", // text to draw
FontFamily.GenericSansSerif, // or any other font family
(int) FontStyle.Regular, // font style (bold, italic, etc.)
g.DpiY * fontSize / 72, // em size
new Point(0, 0), // location where to draw text
new StringFormat()); // set options here (e.g. center alignment)
g.DrawPath(Pens.Black, p);
// + g.FillPath if you want it filled as well
关于c# - 用System.Drawing大纲文本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4200843/