using System.Drawing;
// 创建画布
Bitmap bitmap = new Bitmap(400, 200);
// 创建画笔及字体
Graphics graphics = Graphics.FromImage(bitmap);
Font font = new Font("Arial", 14);
Brush brush = new SolidBrush(Color.Black);
// 绘制文字
graphics.DrawString("Hello World!", font, brush, new Point(50, 50));
// 保存为图片
bitmap.Save("text.png");
下面展示winform中的一个示例:
if (string.IsNullOrWhiteSpace(ztxtcon.Text)) { MessageBox.Show("未输入内容"); return; }
Graphics ghcs = this.CreateGraphics();
SaveFileDialog save = new SaveFileDialog();
save.Filter = "图像(*.jpg)|*.jpg";
Bitmap bitmap = new Bitmap(ztxtcon.Width, ztxtcon.Height);
ghcs = Graphics.FromImage(bitmap);
ghcs.FillRectangle(new SolidBrush(Color.White), 0, 0, ztxtcon.Width, ztxtcon.Height);
if (save.ShowDialog() == DialogResult.OK)
{
ghcs.DrawString(ztxtcon.Text, new Font("宋体", 10), new SolidBrush(Color.Black), new Point(0, 2));
bitmap.Save(save.FileName, ImageFormat.Bmp);
ghcs.Dispose();
bitmap.Dispose();
MessageBox.Show("保存成功");
}