C#.NET 合並圖片


 

引用: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);
            }

 

 

--


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM