C#合并两个图片,并保存到本地

需求:从数据库中查询出两张图片的相对路径,其中一个为底图,另一个图片需附着在底图上,最终合成为一张图片并保存到项目的Test文件夹中,图片以公司名称分类,每个公司的图片放到一个文件夹中。

数据库信息如下:

代码实现:

            try
            {
                //数据库查询图片信息
                using (OracleConnection conn = new OracleConnection(DefaultDBConnection))
                {
                    conn.Open();//打开连接
                    string SQL = @"select * from (
                                    select t1.BLOCKNAME,substr(BPHOTOPATH,instr(BPHOTOPATH,'/',1,3)+1,instr(BPHOTOPATH,'/',1,4)-instr(BPHOTOPATH,'/',1,3)-1) corp,
                                    t.BPHOTOPATH,t.PPHOTOPATH from  as_land_photo t 
                                    left join view_dw_landinfo t1 on t.land_id=t1.PID
                                    ) order by corp";
                    OracleCommand cmd = new OracleCommand(SQL, conn);
                    OracleDataAdapter vDa = new OracleDataAdapter(cmd);
                    DataSet vDs = new DataSet();
                    vDa.Fill(vDs);
                    DataTable dt = vDs.Tables[0];

                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            string saveFile = PathConvert("Test/" + dt.Rows[i]["CORP"] + "/" + dt.Rows[i]["BLOCKNAME"] + ".jpg");//图片文件名称
                            string imgBackPath = PathConvert(dt.Rows[i]["BPHOTOPATH"].ToString()); //底图路径
                            string imgBackExtension = Path.GetExtension(imgBackPath);

                            string imgPath = PathConvert(dt.Rows[i]["PPHOTOPATH"].ToString()); //图片路径
                            string imgExtension = Path.GetExtension(imgPath);

                            if (File.Exists(imgBackPath) == false || File.Exists(imgPath) == false || (imgBackExtension != ".png" && imgBackExtension != ".jpg") || (imgBackExtension != ".png" && imgBackExtension != ".jpg"))
                            {
                                logger.Info("文件不存在或不支持该扩展名!");
                            }

                            //在调用 Save 方法之前,先判断目录是否存在,若不存在,则创建。
                            string filePath = PathConvert("Test/" + dt.Rows[i]["CORP"]);//图片文件加名称
                            if (!Directory.Exists(filePath))
                            {
                                Directory.CreateDirectory(filePath);
                            }

                            Image imgBack = Image.FromFile(imgBackPath);
                            Image img = Image.FromFile(imgPath);

                            Bitmap bmp = CombinImage(imgBack, img);
                            bmp.Save(saveFile, ImageFormat.Jpeg);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                
            }
        #region 合并图片
        /// <summary>
        /// 合并图片
        /// </summary>
        /// <param name="imgBack"></param>
        /// <param name="img"></param>
        /// <param name="xDeviation"></param>
        /// <param name="yDeviation"></param>
        /// <returns></returns>
        public static Bitmap CombinImage(Image imgBack, Image img, int xDeviation = 0, int yDeviation = 0)
        {
            Bitmap bmp = new Bitmap(imgBack.Width, imgBack.Height);

            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);

            g.DrawImage(img, imgBack.Width / 2 - img.Width / 2 + xDeviation, imgBack.Height / 2 - img.Height / 2 + yDeviation, img.Width, img.Height);
            GC.Collect();
            return bmp;
        }
        #endregion


        #region 将相对路径转换成绝对路径
        /// <summary>
        /// 将相对路径转换成绝对路径
        /// </summary>
        /// <param name="strPath">相对路径</param>
        public static string PathConvert(string strPath)
        {
            //web程序使用
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Server.MapPath(strPath);
            }
            else //非web程序引用
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("\\"))
                {
                    strPath = strPath.TrimStart('\\');
                }
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
            }
        }
        #endregion

程序执行结果:

在项目中生成Test文件夹

在Test文件夹中根据公司名称生成了对应的文件夹

 每个公司文件夹中生成对应公司的图片

参考链接:https://www.cnblogs.com/taidou/p/7211440.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值