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);
Yes. Instead of DrawString, use the following sequence of calls:
new GraphicsPath
(creates an emptyGraphicsPath
)GraphicsPath.AddString
(theGraphicsPath
object now represents the outline of the text)Graphics.DrawPath
(draws the outline in anyPen
you want)If you need to use
GraphicsPath.AddString
alongsideGraphics.DrawString
, you need to convert the font sizes, becauseGraphics.DrawString
expects “point size” whileGraphicsPath.AddString
expects “em size”. The conversion formula is simplyemSize = g.DpiY * pointSize / 72
.Here's a code example: