dotNet疯狂之路No.30
今天很残酷,明天更残酷,后天很美好,但是绝大部分人是死在明天晚上,只有那些真正的英雄才能见到后天的太阳。
We're here to put a dent in the universe. Otherwise why else even be here?
前几天公司急需一批台卡 生成带参数二维码 和背景图片合为一张图片 合成图片还需要加上水印 开始是美工一张张做 几千张哈哈累死美工了 老板要求写个生成功能 所以我就试水了
一、生成二维码部分(一次生成几千张 先保存下来备用)
/// <summary> /// 保存批次二维码至本地//一次生成几千张 先保存下来备用 /// </summary> /// <param name="code"></param> /// <param name="name"></param> public static void setBatchImg(string strCode, string batch,int i) { Bitmap bitmap = QRCodeEncoderUtil(strCode);//调用二维码编码方法生成位图 var ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Png); String relPath = "/XXXX/XXXX/XXXX/XXXX" + batch + "/";//文件夹路径 string path = System.Web.HttpContext.Current.Server.MapPath(relPath); if (!System.IO.Directory.Exists(path)) //如果文件夹不存在则创建 { System.IO.Directory.CreateDirectory(path); } Log.WriteLog(path + batch + "_" + i + ".png"); bitmap.Save(path + batch + "_" + i + ".png", ImageFormat.Png);///二维码保存 } /// <summary> /// 生成二维码 /// </summary> /// <param name="qrCodeContent">要编码的内容</param> /// <returns>返回二维码位图</returns> public static Bitmap QRCodeEncoderUtil(string qrCodeContent) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeVersion = 0;/// 数字越大二维码密集度越高 Bitmap img = qrCodeEncoder.Encode(qrCodeContent, Encoding.UTF8);//指定utf-8编码, 支持中文 return img; }
二、调整二维码大小(适用背景图片上二维码区域)
string qrPath="XXXX/XXXX/XXXX/XXXX.png"; Bitmap b = new Bitmap(qrPath); System.Drawing.Image i = resizeImage(b, new Size(width, height));// 调用调整方法进行调整 返回Image 保存至本地备用 string rePath = Server.MapPath("XXXXX") + "\XXXX\reqr.png"; i.Save(rePath); i.Dispose();
//调整图像大小 private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size) { //获取图片宽度 int sourceWidth = imgToResize.Width; //获取图片高度 int sourceHeight = imgToResize.Height; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; //计算宽度的缩放比例 nPercentW = ((float)size.Width / (float)sourceWidth); //计算高度的缩放比例 nPercentH = ((float)size.Height / (float)sourceHeight); if (nPercentH < nPercentW) nPercent = nPercentH; else nPercent = nPercentW; //期望的宽度 int destWidth = (int)(sourceWidth * nPercent); //期望的高度 int destHeight = (int)(sourceHeight * nPercent); Bitmap b = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage((System.Drawing.Image)b); g.InterpolationMode = InterpolationMode.HighQualityBicubic; //绘制图像 g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); g.Dispose(); imgToResize.Dispose(); return (System.Drawing.Image)b; }
三、合成图片二维码和背景合为一张图片
//图片合成函数(这里合成两张图片 可累加) private void MergeImage(string strBg, string strQr, string name) { int x = 0; int y = 0; if (Custom.Value.ToString().Equals("1"))//是否开启自定义 { x = int.Parse(imgx.Value) * 2; y = int.Parse(imgy.Value) * 2; } else { x = int.Parse(imgx.Value); y = int.Parse(imgy.Value); } // 数组元素个数(即要拼图的图片个数) int lenth = 2; // 图片集合 Bitmap[] maps = new Bitmap[lenth]; //图片对应纵坐标集合 int[] pointY = new int[lenth]; //读取本地图片初始化Bitmap Bitmap map = null; //第一个图片对象,背景图片 map = new Bitmap(strBg); maps[0] = map; pointY[0] = 0; //第二个图片对象,二维码 map = new Bitmap(strQr); maps[1] = map; pointY[1] = y; // 初始化背景图片的宽高 Bitmap bitMap = new Bitmap(maps[0].Width, maps[0].Height); // 初始化画板 Graphics g1 = Graphics.FromImage(bitMap); //绘制第一个图片,背景图 for (int i = 0; i < maps[0].Width; i++) { for (int j = 0; j < maps[0].Height; j++) { // 以像素点形式绘制(将要拼图的图片上的每个坐标点绘制到拼图对象的指定位置,直至所有点都绘制完成) var temp = maps[0].GetPixel(i, j); // 将图片画布的点绘制到整体画布的指定位置 bitMap.SetPixel(i, pointY[0] + j, temp); } } maps[0].Dispose(); //绘制第二个图片,二维码 for (int i = 0; i < maps[1].Width; i++) { for (int j = 0; j < maps[1].Height; j++) { var temp = maps[1].GetPixel(i, j); bitMap.SetPixel(x + i, pointY[1] + j, temp); } } maps[1].Dispose();
string batch = batchCode.Value.ToString(); // 保存输出到本地 if (!System.IO.Directory.Exists(Server.MapPath("XXXX") + "/XXXX/XXXX" + batch + "/")) //如果文件夹不存在则创建 { System.IO.Directory.CreateDirectory(Server.MapPath("XXXX") + "/XXXX/XXXX" + batch + "/"); } bitMap.Save(Server.MapPath("XXXX") + "/XXXX/XXXX" + batch + "/" + batch + "_" + name + ".png"); g1.Dispose(); bitMap.Dispose(); }
四、添加水印
/// <summary> /// 水印文字 /// </summary> /// <param name="fileName"></param> /// <param name="text"></param> private void AddTextToImg(string fileName, string text) { int x = 0; int y = 0; if (Custom.Value.ToString().Equals("1"))//是否开启自定义 { x = int.Parse(ztimgx.Value) * 2; y = int.Parse(ztimgy.Value) * 2; } else { x = int.Parse(ztimgx.Value); y = int.Parse(ztimgy.Value); } if (text == string.Empty) { return; } //还需要判断文件类型是否为图像类型,这里就不赘述了 System.Drawing.Image image = System.Drawing.Image.FromFile(fileName); Bitmap bitmap = new Bitmap(image, image.Width, image.Height); Graphics g = Graphics.FromImage(bitmap); float fontSize = 55.0f; //字体大小 float textWidth = text.Length * fontSize; //文本的长度 //下面定义一个矩形区域,以后在这个矩形里画上白底黑字 float rectX = x; float rectY = y; float rectWidth = text.Length * (fontSize); float rectHeight = fontSize + 18; //声明矩形域 RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight); Font font = new Font("宋体", fontSize); //定义字体 Brush whiteBrush = new SolidBrush(Color.Black); //黑笔刷,画文字用 Brush blackBrush = new SolidBrush(Color.White); //白笔刷,画背景用 g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight); g.DrawString(text, font, whiteBrush, textArea); string batch = batchCode.Value.ToString(); // 保存输出到本地 if (!System.IO.Directory.Exists(Server.MapPath("XXXX") + "/XXXX/XXXX" + batch + "/")) //如果文件夹不存在则创建 { System.IO.Directory.CreateDirectory(Server.MapPath("XXXX") + "/XXXX/XXXX" + batch + "/"); } bitmap.Save(Server.MapPath("XXXX") + "/XXXX/XXXXX" + batch + "/" + batch + "_" + text + ".png"); g.Dispose(); bitmap.Dispose(); image.Dispose(); }
五、调取所有方法进行合成
/// <summary> /// 生成批次台卡生成压缩包 /// </summary> private void CreateBatchImg() { string batch = "XXXXX.png"; string bgPath = ""; int width = 0; int height = 0; for (int j = 0; j < 3000; j++) { setBatchImg("生成二维码的参数", batch, j);///保存图片至本地 string qrPath = Server.MapPath("XXXX") + "\XXXX\XXXX" + batch + "\" + batch + "_" + j + ".png"; bgPath = Server.MapPath("XXXXX") + "\" +XXXX + ".png"; width = int.Parse(imgwith.Value); height = int.Parse(imgheight.Value); //调整二维码大小 Bitmap b = new Bitmap(qrPath); System.Drawing.Image i = resizeImage(b, new Size(width, height)); string rePath = Server.MapPath("XXXX") + "\XXXX\reqr.png"; i.Save(rePath); i.Dispose(); MergeImage(bgPath, rePath, j.ToString()); AddTextToImg(Server.MapPath("Images") + "/TemQrErBatch" + batch + "/" + batch + "_" + j + ".png", j.ToString()); } } string relPath = "/XXXX/XXXX/XXXX/XXXXX" + batch + "/"; string relPath2 = "/XXXX/XXXX/XXXX/"; string path = Server.MapPath(relPath); string path2 = Server.MapPath(relPath2);//压缩路径 if (!Directory.Exists(path)) { // Create the directory it does not exist. Directory.CreateDirectory(path); } if (System.IO.Directory.Exists(path)) //如果文件夹不存在则创建 { string[] FileProperties = new string[2]; FileProperties[0] = path;//待压缩文件目录 FileProperties[1] = path2 + "XXXX" + batch + ".zip"; //压缩后的目标文件 ZipFloClass.ZipFiles(FileProperties[0], FileProperties[1]); } ZipUrl.Value = "XXXX/XXXX/XXXX" + batch + ".zip"; }