using System.Drawing;
using System.Drawing.Drawing2D;
public Image CreateTextImage(string text, Font font, Color backgroundColor, Color foreColor,
int margin = 5, RotateFlipType rotate = RotateFlipType.RotateNoneFlipNone)
{
SizeF sizeF = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(text, font);
Bitmap bitmap = new Bitmap((int)sizeF.Width + margin * 2, (int)sizeF.Height + margin * 2);
Graphics g = Graphics.FromImage(bitmap);
g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.HighQuality;
g.FillRegion(new SolidBrush(backgroundColor), g.Clip);
g.DrawString(text, font, new SolidBrush(foreColor), margin, margin);
g.Dispose();
bitmap.RotateFlip(rotate);
return bitmap;
}