using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Text; using System.IO; using System.Linq; using System.Net; using System.Web; using YFAPICommon.Controllers; namespace YFAPICommon.Libs { public class YFFotoMix { private static string serverPath = System.Configuration.ConfigurationSettings.AppSettings["serverPath"]; private static string localPath = System.Configuration.ConfigurationSettings.AppSettings["localPath"]; /// <summary> /// 合成用户分享图片 /// </summary> /// <returns></returns> public static string CombinUserShareImg(string str,string imgUrl,string wxPath,string fileName) { const string folderPath = "\\shareimg\\"; const string serverFolderPath = "/shareimg/"; string folder = localPath + folderPath; //判断文件的存在 if (System.IO.File.Exists(Path.Combine(folder, fileName+".jpg"))) { //存在文件 return serverPath + serverFolderPath + fileName + ".jpg"; } Image img1 = Image.FromFile(Path.Combine(folder, "sharetemp1.jpg"));//相框图片 Image img2 = DrawTransparentRoundCornerImage(downloadImage(imgUrl),30); //照片图片 Image wxQrCode = DrawTransparentRoundCornerImage(WXController.getWXCodeImage(wxPath)); //从指定的System.Drawing.Image创建新的System.Drawing.Graphics Graphics g = Graphics.FromImage(img1); g.DrawImage(img1, 0, 0, img1.Size.Width, img1.Size.Height);// g.DrawImage(imgBack, 0, 0, 相框宽, 相框高); //g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一层黑色边框 //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高); g.DrawImage(img2, 75, 243, 650, 838);//户型图 g.DrawImage(wxQrCode, 540, 1175, 200, 200);//小程序二维码 //************************************ g.TextRenderingHint = TextRenderingHint.AntiAlias; FontFamily fontFamily = new FontFamily("黑体"); Font font1 = new Font(fontFamily, 28f, FontStyle.Regular, GraphicsUnit.Pixel); string poem1 = str; g.DrawString(poem1, font1, Brushes.White, 70, 1320); //************************************ GC.Collect(); img1.Save(Path.Combine(folder, fileName + ".jpg")); img1.Dispose(); return serverPath+ serverFolderPath+ fileName + ".jpg"; } public static string CombinUserShareImg2(string wxPath, string fileName) { const string folderPath = "\\shareimg\\"; const string serverFolderPath = "/shareimg/"; string folder = localPath + folderPath; //判断文件的存在 if (System.IO.File.Exists(Path.Combine(folder, fileName + ".jpg"))) { //存在文件 return serverPath + serverFolderPath + fileName + ".jpg"; } Image img1 = Image.FromFile(Path.Combine(folder, "sharetemp2.jpg"));//相框图片 Image wxQrCode = DrawTransparentRoundCornerImage(WXController.getWXCodeImage(wxPath)); //从指定的System.Drawing.Image创建新的System.Drawing.Graphics Graphics g = Graphics.FromImage(img1); g.DrawImage(img1, 0, 0, img1.Size.Width, img1.Size.Height);// g.DrawImage(imgBack, 0, 0, 相框宽, 相框高); //g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一层黑色边框 //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高); g.DrawImage(wxQrCode, 590, 1225, 160, 160);//小程序二维码 //************************************ GC.Collect(); img1.Save(Path.Combine(folder, fileName + ".jpg")); img1.Dispose(); return serverPath + serverFolderPath + fileName + ".jpg"; } //图片处理为圆角 private static System.Drawing.Image DrawTransparentRoundCornerImage(System.Drawing.Image image,int radius) { Bitmap bm = new Bitmap(image.Width, image.Height); Graphics g = Graphics.FromImage(bm); g.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, image.Width, image.Height)); using (System.Drawing.Drawing2D.GraphicsPath path = CreateRoundedRectanglePath(new Rectangle(0, 0, image.Width, image.Height), radius)) { g.SetClip(path); } g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); g.Dispose(); return bm; } //图片处理为圆形 private static System.Drawing.Image DrawTransparentRoundCornerImage(System.Drawing.Image image) { Bitmap bm = new Bitmap(image.Width, image.Height); Graphics g = Graphics.FromImage(bm); g.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, image.Width, image.Height)); using (System.Drawing.Drawing2D.GraphicsPath path = CreateRoundedRectanglePath(new Rectangle(0, 0, image.Width, image.Height), image.Width / 2)) { g.SetClip(path); } g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); g.Dispose(); return bm; } //设置图片四个边角弧度 private static System.Drawing.Drawing2D.GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius) { System.Drawing.Drawing2D.GraphicsPath roundedRect = new System.Drawing.Drawing2D.GraphicsPath(); roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90); roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y); roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90); roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2); roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90); roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom); roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90); roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2); roundedRect.CloseFigure(); return roundedRect; } private static Image downloadImage(string url) { WebRequest imgRequest = WebRequest.Create(url); HttpWebResponse res; try { res = (HttpWebResponse)imgRequest.GetResponse(); } catch (WebException ex) { res = (HttpWebResponse)ex.Response; } if (res.StatusCode.ToString() == "OK") { System.Drawing.Image downImage = System.Drawing.Image.FromStream(imgRequest.GetResponse().GetResponseStream()); return downImage; } return null; } } }