需求分析:
用户使用固定图片模板,录入推广文字生成图片。
使用GDI+ 画图 Graphics
具体实现
图片模板
业务代码
//录入文字
var text="";
//图片模板
var imgPath = @"mb.png";
System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(imgPath);
using (Graphics g = Graphics.FromImage(imgSrc))
{
g.DrawImage(imgSrc, 0, 0, imgSrc.Width, imgSrc.Height);
using (Font f = new Font("宋体", 10))
{
using (Brush b = new SolidBrush(Color.Black))
{
SizeF sf = g.MeasureString(text, f, int.Parse(fontWidth));
RectangleF rf = new RectangleF(float.Parse(X), float.Parse(Y), sf.Width, sf.Height);
g.DrawString(text, f, b, rf);
}
}
}
string directory = @"D:\img\";
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
string filePath = directory + fileName;
FileStream fileStream = File.Create(filePath);
fileStream.Close();
imgSrc.Save(filePath, System.Drawing.Imaging.ImageFormat.Bmp);
效果图