使用System.Drawing概述文本?

时间:2010-11-17 02:00:56

标签: c# asp.net-mvc-2 graphics system.drawing

我有以下代码。有没有简单的方法来概述我正在写的文字?

 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);

1 个答案:

答案 0 :(得分:26)

是。而不是DrawString,使用以下调用序列:

如果您需要在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