Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following code. Is there an easy way to put an outline on the text I am writing?

 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
486 views
Welcome To Ask or Share your Answers For Others

1 Answer

Yes. Instead of DrawString, use the following sequence of calls:

If you need to use GraphicsPath.AddString alongside Graphics.DrawString, you need to convert the font sizes, because Graphics.DrawString expects “point size” while GraphicsPath.AddString expects “em size”. The conversion formula is simply emSize = g.DpiY * pointSize / 72.

Here's a code example:

// 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...