我有以下代码。有没有简单的方法来概述我正在写的文字?
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);
答案 0 :(得分:26)
是。而不是DrawString,使用以下调用序列:
new GraphicsPath
(创建一个空的GraphicsPath
)GraphicsPath.AddString
(GraphicsPath
对象现在代表文字的大纲)Graphics.DrawPath
(在您想要的任何Pen
中绘制大纲)如果您需要在GraphicsPath.AddString
旁边使用Graphics.DrawString
,则需要转换字体大小,因为Graphics.DrawString
需要“磅值”,而GraphicsPath.AddString
需要“大小” 。转换公式只是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