runliuv

runliuv@cnblogs

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

引用:https://www.cnblogs.com/stulzq/p/6137715.html

 

util:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace DotNet合并图片.Utils
{
   public static class ImageUtil
    {

        /// <summary>  
        /// 合并图片,默认是垂直合并,图1在上,图2在下。
        /// </summary>  
        /// <param name="imgBack"></param>  
        /// <param name="img"></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 + img.Height);

            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);     

            //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框    

            //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);    

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


    }
}

 

使用:

try
            {
                string iamge1FullName = Path.Combine(Application.StartupPath, "1.png");
                string iamge2FullName = Path.Combine(Application.StartupPath, "2.png");
                Image img1 = Image.FromFile(iamge1FullName);
                Image img2 = Image.FromFile(iamge2FullName);


                string iamgeAllFullName = Path.Combine(Application.StartupPath, "all.png");

                Bitmap bmAll = DotNet合并图片.Utils.ImageUtil.CombinImage(img1, img2);

                bmAll.Save(iamgeAllFullName, ImageFormat.Png);

                MessageBox.Show("完成!");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

 

 

--

posted on 2019-11-22 14:58  runliuv  阅读(1441)  评论(0编辑  收藏  举报