/// <summary>
/// 指定图片添加指定文字
/// </summary>
/// <param name="text">添加的文字</param>
/// <param name="picname">生成文件名</param>
private void AddTextToImg(string cardno,string name)
{
//判断指定图片是否存在
Image image = Image.FromFile("D://card.jpg");
Bitmap bitmap = new Bitmap(image, image.Width, image.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//字体大小
float fontSize = 20.0f;
//文本的长度
float textWidth = cardno.Length * fontSize;
//下面定义一个矩形区域,以后在这个矩形里画上白底黑字
float rectX = 100;
float rectY = 500;
float rectWidth = cardno.Length * (fontSize + 20);
float rectHeight = fontSize + 20;
//声明矩形域
RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);
//定义字体
System.Drawing.Font font = new System.Drawing.Font("微软雅黑", fontSize, System.Drawing.FontStyle.Regular);
//font.Bold = true;
//白笔刷,画文字用
Brush whiteBrush = new SolidBrush(System.Drawing.Color.Black);
//黑笔刷,画背景用
//Brush blackBrush = new SolidBrush(Color.Black);
//g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);
g.DrawString(cardno, font, whiteBrush, textArea);
//输出方法一:将文件生成并保存到D盘
string path = "D://Cards/" + name + ".jpg";
bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
bitmap.Dispose();
image.Dispose();
}
备注:支持空格
共有条评论 网友评论