public static System.Drawing.Image GenerateGiftCard(String text, Font font, Color textColor)
{
System.Drawing.Image img = Bitmap.FromFile(@"G:\xxx\images\gift-card.jpg");
Graphics drawing = Graphics.FromImage(img);
//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, font);
//create a brush for the text
Brush textBrush = new SolidBrush(textColor);
float x, y;
x = img.Width / 2 - textSize.Width / 2;
y = img.Height / 2 - textSize.Height / 2;
drawing.DrawString(text, font, textBrush, x, y);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
}
But the text generated by this code is "plain" not dimensional and no shadow beneath it.
This is the font style I want:
Is there anything I can do to generate the same style via my code?
Does anyone know how to use SiteMapPath or ResolveURL objects to transfer a relative path to a physical one? cheers,
First render the shadow by drawing the text with a darker, optionally translucent brush at an offset. After the shadow is rendered, overlay the regular text.
Example:
public static System.Drawing.Image GenerateGiftCard(String text, Font font, Color textColor, Color shadowColor, SizeF shadowOffset)
{
System.Drawing.Image img = Bitmap.FromFile(@"G:\xxxx\images\gift-card.jpg");
Graphics drawing = Graphics.FromImage(img);
//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, font);
//create a brush for the text
Brush shadowBrush = new SolidBrush(shadowColor); // <-- Here
Brush textBrush = new SolidBrush(textColor);
float x, y;
x = img.Width / 2 - textSize.Width / 2;
y = img.Height / 2 - textSize.Height / 2;
drawing.DrawString(text, font, shadowBrush, x + shadowOffset.Width, y + shadowOffset.Height); // <-- Here
drawing.DrawString(text, font, textBrush, x, y);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With