private static string DrawText(String text)
{
Font font = new Font("Arial", 1024, FontStyle.Bold);
Color textColor = Color.White;
Color backColor = Color.FromArgb(1, 143, 36, 174);
//first, create a dummy bitmap just to get a graphics object
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, font);
//free up the dummy image and old graphics object
img.Dispose();
drawing.Dispose();
//create a new image of the right size
img = new Bitmap((int)textSize.Width, (int)textSize.Height);
drawing = Graphics.FromImage(img);
drawing.SmoothingMode = SmoothingMode.HighQuality;
drawing.InterpolationMode = InterpolationMode.HighQualityBicubic;
drawing.PixelOffsetMode = PixelOffsetMode.HighQuality;
//paint the background
drawing.Clear(backColor);
Brush rect = new SolidBrush(backColor);
Rectangle rectangle = new Rectangle(0, 0, 200, 200);
drawing.FillRectangle(rect, rectangle);
//create a brush for the text
Brush textBrush = new SolidBrush(textColor);
drawing.DrawString(text, font, textBrush, 0, 0);
drawing.Save();
string base64;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, ImageFormat.Jpeg);
base64 = Convert.ToBase64String(ms.ToArray());
}
textBrush.Dispose();
drawing.Dispose();
return base64;
}