C#利用Bitmap和Graphics合成文字图片,新建窗体,在窗体中添加一个button,button中代码如下
private void button1_Click(object sender, EventArgs e)
{
string text = "江湖渐远";//预设要合成的字符串
Bitmap bmp = new Bitmap(500, 300);//设置图片大小
Graphics g =Graphics .FromImage(bmp);//封装一个GDI+绘图图画
Random r = new Random();//产生随机数获取随机颜色
g.Clear (Color .White );//设置背景色为白色
for (int j = 0; j < 4; j++)
{
string[] fonts = { "微软雅黑", "Viner Hand ITC", "Tempus Sans ITC", "汉仪长艺体简", "汉仪双线体简", "汉仪花蝶体简" };//预置字体列表
Point p = new Point((j + 1) * 78, 60);//每个汉字的坐标
Color[] colors = { Color.Red, Color.Green, Color.Black, Color.Yellow, Color.LightSkyBlue, Color.Blue };//预置颜色列表
g.DrawString(text[j].ToString(), new Font(fonts[r.Next(fonts.Length)], 40, FontStyle.Bold), new SolidBrush(colors[r.Next(colors.Length)]), p);//画图
}
pictureBox1.Image = bmp;//在pictureBox中预览图片
string path = Application.StartupPath + "/123.jpg"; //保存路径
bmp .Save (path,System.Drawing.Imaging.ImageFormat.Png );//保存图片
}