winform 图片内容替换指定文字

38 阅读1分钟

图片模板替换,包含了替换文字及二维码生成等信息

public static void imageReplace(){
  string path = "D://template.jpg";
    // 读取本地模板图片
    Image image = Image.FromFile(path);
    // 模板图片的大小,看属性-详情-分辨率
    var imagedata = new Bitmap(image, 995, 1722);
    var sourcegra = Graphics.FromImage(imagedata);//存入画布
    sourcegra.DrawString(point.DateStr, new System.Drawing.Font("黑体", 11, FontStyle.Regular), new SolidBrush(Color.White), 223, 200);
    StringFormat strFormat1 = new StringFormat();
    strFormat1.Alignment = StringAlignment.Far;
    strFormat1.Trimming = StringTrimming.Word;
    // 可在画图工具中打开模板图片,根据鼠标位置进行设置,然后再微调,以下RectangleF可作为隐形方框进行右对齐
    sourcegra.DrawString(point.Address, new System.Drawing.Font("黑体", 11, FontStyle.Regular), new SolidBrush(Color.FromArgb(77, 77, 77)), new RectangleF(106, 462, 850, 100), strFormat1);
    // 生成二维码方式
    sourcegra.DrawImage(imagecode(point.QrCode, 5, 5, "", 0, 0, false), 630, 1482, 170, 170);//绘制二维码到画布
    imagedata.Save("new.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);//保存图片到D盘
}
    
    
public static Bitmap imagecode(string msg, int version, int pixel, string icon_path, int icon_size, int icon_border, bool white_edge)
{
    // 5这个是可容纳字符串大小,自己根据实际内容长度设置合适的值
    QRCoder.QRCodeData code_data = new QRCoder.QRCodeGenerator().CreateQrCode(msg, QRCoder.QRCodeGenerator.ECCLevel.M, true, true, QRCoder.QRCodeGenerator.EciMode.Utf8, 5);
    QRCoder.QRCode code = new QRCoder.QRCode(code_data);
    return code.GetGraphic(pixel, Color.Black, Color.White, white_edge);
}