图片合成YFFotoMix

简介: 图片合成YFFotoMix
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;
        }
    }
}
相关文章
|
7月前
gif录屏与gif图片合成工具
gif录屏与gif图片合成工具
38 2
|
7月前
|
人工智能 搜索推荐 定位技术
证件照尺寸修改、图片背景换色、照片大小压缩…几个在线图片编辑、处理网站推荐
证件照尺寸修改、图片背景换色、照片大小压缩…几个在线图片编辑、处理网站推荐
177 1
|
数据采集 Java Web App开发
爬取王者荣耀皮肤图片
用爬虫相关知识,进行爬取王者荣耀皮肤图片。
1201 1
|
数据采集 Java Web App开发
爬取英雄联盟皮肤图片
使用爬虫相关知识,进行爬取英雄联盟皮肤图片。
190 0
|
前端开发
canvas实现海报 两张图片合成一张并且可以保存
canvas实现海报 两张图片合成一张并且可以保存
canvas实现海报 两张图片合成一张并且可以保存
|
前端开发 算法
制作了一个马赛克图片转换器 - 实现篇
上文有讲到我制作了一个马赛克图片转换器,可以将图片转换成马赛克风格,并可转换为 css box-shadow 进行输出。
|
前端开发
制作了一个马赛克图片转换器
制作了一个马赛克图片转换器,可以将图片转换成马赛克风格,并可转换为 css box-shadow 进行输出。
|
Linux 数据安全/隐私保护 Windows
Stegosuite,图片隐写术
版权声明:转载请注明出处:http://blog.csdn.net/dajitui2024 https://blog.csdn.net/dajitui2024/article/details/79396511 ...
1414 0
|
计算机视觉
opencv,图片合成
opencv,图片合成
665 0
|
小程序 JavaScript 程序员
【开源】【猫咪卡通变 - 小程序】拍摄猫咪或上传猫咪照片,使其转化为卡通猫咪.(且上传图片必须为猫咪)
废话不多说,直接看吧! 涉及技术:微信小程序云开发 涉及API接口:百度云-图像增强、百度云-图像识别
341 0
【开源】【猫咪卡通变 - 小程序】拍摄猫咪或上传猫咪照片,使其转化为卡通猫咪.(且上传图片必须为猫咪)