生成带二维码的图片
/// <summary> /// 生成带二维码的图片 /// 所在目录下imgs目录 分为 八张切图 /// 名称:imgTop、imgTopLeft、imgHeadPhoto、imgTopRight、imgIn、imgLeft、imgRight、imgBotton /// 格式:jpg /// </summary> /// <param name="qrcodeUri">二维码名称</param> /// <returns>生成的图片地址</returns> public string getQrCodeImg(string qrcodeUri,string where,int hasPhoto) { //根据 邀请码 获取当前用户 昵称、头像 BLL.sf_user_info userBll = new BLL.sf_user_info(); Model.sf_user_info userInfo = userBll.GetModelByKey(qrcodeUri,where); System.Drawing.Image imgHeadPhoto = System.Drawing.Image.FromFile(Utils.GetMapPath(@"/allqrcode/imgs/imgHeadPhoto.jpg")); int headwidth = imgHeadPhoto.Width; int headheight = imgHeadPhoto.Height; try { if (!string.IsNullOrEmpty(userInfo.photoUri) && hasPhoto>0) { System.Net.WebRequest request = System.Net.WebRequest.Create(userInfo.photoUri); System.Net.WebResponse response = request.GetResponse(); Stream reader = response.GetResponseStream(); if (reader != null) { imgHeadPhoto = System.Drawing.Image.FromStream(reader); } reader.Close(); reader.Dispose(); } } catch (Exception) { } //根据 二维码资源 参数 获取图片对象 System.Drawing.Image imgCode = System.Drawing.Image.FromFile(Utils.GetMapPath(@"/allqrcode/code/" + qrcodeUri + ".jpg")); //从固定目录读取 图片所需切片 System.Drawing.Image imgTop = System.Drawing.Image.FromFile(Utils.GetMapPath(@"/allqrcode/imgs/imgTop.jpg")); System.Drawing.Image imgTopLeft = System.Drawing.Image.FromFile(Utils.GetMapPath(@"/allqrcode/imgs/imgTopLeft.jpg")); System.Drawing.Image imgTopRight = System.Drawing.Image.FromFile(Utils.GetMapPath(@"/allqrcode/imgs/imgTopRight.jpg")); System.Drawing.Image imgIn = System.Drawing.Image.FromFile(Utils.GetMapPath(@"/allqrcode/imgs/imgIn.jpg")); System.Drawing.Image imgLeft = System.Drawing.Image.FromFile(Utils.GetMapPath(@"/allqrcode/imgs/imgLeft.jpg")); System.Drawing.Image imgRight = System.Drawing.Image.FromFile(Utils.GetMapPath(@"/allqrcode/imgs/imgRight.jpg")); System.Drawing.Image imgBotton = System.Drawing.Image.FromFile(Utils.GetMapPath(@"/allqrcode/imgs/imgBotton.jpg")); //创建 带二维码的图片 大小的 位图 Bitmap tmpImage = new Bitmap(imgTop.Width, imgTop.Height + imgTopLeft.Height + imgIn.Height + imgLeft.Height + imgBotton.Height); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(tmpImage); //绘制文字水印 if (hasPhoto>0) { System.Drawing.Graphics gs = System.Drawing.Graphics.FromImage(imgIn); Brush red = new System.Drawing.Drawing2D.LinearGradientBrush( new Rectangle(10, 20, 240, 30), Color.Red, Color.Red, 90); gs.DrawString(userInfo.nickName, new Font("华文楷体", 25, FontStyle.Bold), red, new PointF(160, 33)); gs.Dispose(); } //在同一位图上 绘制 不同的图片内容 //绘制头部 g.DrawImage(imgTop, 0, 0, imgTop.Width, imgTop.Height); //绘制头像这一行 g.DrawImage(imgTopLeft,0, imgTop.Height, imgTopLeft.Width, imgTopLeft.Height); g.DrawImage(imgHeadPhoto, imgTopLeft.Width, imgTop.Height, headwidth, headheight); g.DrawImage(imgTopRight, imgTopLeft.Width + headwidth, imgTop.Height, imgTopRight.Width, imgTopRight.Height+4); //绘制中间 g.DrawImage(imgIn, 0, imgTop.Height + imgTopLeft.Height, imgIn.Width, imgIn.Height); //绘制二维码这一行 g.DrawImage(imgLeft, 0, imgTop.Height + imgTopLeft.Height + imgIn.Height, imgLeft.Width, imgLeft.Height); g.DrawImage(imgCode, imgLeft.Width, imgTop.Height + imgTopLeft.Height + imgIn.Height, imgLeft.Height, imgLeft.Height); g.DrawImage(imgRight, imgLeft.Width + imgLeft.Height+2, imgTop.Height + imgTopLeft.Height + imgIn.Height, imgRight.Width, imgRight.Height); //绘制底部 g.DrawImage(imgBotton, 0, imgTop.Height + imgTopLeft.Height + imgIn.Height + imgLeft.Height, imgBotton.Width, imgBotton.Height); //释放资源 并保存要返回 位图 imgTop.Dispose(); imgLeft.Dispose(); imgRight.Dispose(); imgTopLeft.Dispose(); imgTopRight.Dispose(); imgIn.Dispose(); imgBotton.Dispose(); imgCode.Dispose(); imgHeadPhoto.Dispose(); string qrCodeImg = "/allqrcode/imgCode/" + qrcodeUri + ".jpg"; tmpImage.Save(Utils.GetMapPath(@qrCodeImg)); g.Dispose(); tmpImage.Dispose(); return qrCodeImg; }
喜欢的朋友请帮忙点个赞!!!