c# 多張圖片合成一張圖片


using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            CombinImage();
        }
      /// <summary>
      /// 方法一
      /// 多張圖片的合並
      /// </summary>
        static private void CombinImage()
        {
            const string folder = @"F:\測試圖片";
            Image img1 = Image.FromFile(Path.Combine(folder, "測試1.png"));
            Bitmap map1 = new Bitmap(img1);
            Image img2 = Image.FromFile(Path.Combine(folder, "測試2.png"));
            Bitmap map2 = new Bitmap(img2);
            Image img3 = Image.FromFile(Path.Combine(folder, "測試3.png"));
            Bitmap map3 = new Bitmap(img3);
            var width = Math.Max(img1.Width, img2.Width);
            var height = img1.Height + img2.Height + 10;
            // 初始化畫布(最終的拼圖畫布)並設置寬高
            Bitmap bitMap = new Bitmap(width, height);
            // 初始化畫板
            Graphics g1 = Graphics.FromImage(bitMap);
            // 將畫布塗為白色(底部顏色可自行設置)
            g1.FillRectangle(Brushes.White, new Rectangle(0, 0, width, height));
            //在x=0,y=0處畫上圖一
            g1.DrawImage(map1, 0, 0, img1.Width, img1.Height);
            //在x=0,y在圖一往下10像素處畫上圖二
            g1.DrawImage(map2, 0, img1.Height + 10, img2.Width, img2.Height);
            g1.DrawImage(map3, 0, img1.Height + 10, img3.Width, img3.Height);
            map1.Dispose();
            map2.Dispose();
            map3.Dispose();
            Image img = bitMap;
            //保存
            img.Save(Path.Combine(folder, "new測試1.png"));
            img.Dispose();
        }

        /// <summary>
        /// 方法二
        ///實現左右拼接圖片
        /// </summary>
        /// <param name="Img1"></param>
        /// <param name="Img2"></param>
        static private void CombinImage2(Image Img1, Image Img2)
        {
#if FALSE  
            //控制台調用
             const string folder = @"F:\測試圖片";
            Image img1 = Image.FromFile(Path.Combine(folder, "測試1.png"));
            Image img2 = Image.FromFile(Path.Combine(folder, "測試2.png"));
            JoinImage(img1, img2);
#endif
            int imgHeight = 0, imgWidth = 0;
            imgWidth = Img1.Width + Img2.Width;
            imgHeight = Math.Max(Img1.Height, Img2.Height);
            Bitmap joinedBitmap = new Bitmap(imgWidth, imgHeight);
            Graphics graph = Graphics.FromImage(joinedBitmap);
            graph.DrawImage(Img1, 0, 0, Img1.Width, Img1.Height);
            graph.DrawImage(Img2, Img1.Width, 0, Img2.Width, Img2.Height);
            Image img = joinedBitmap;
            //保存
            const string folder = @"F:\測試圖片";
            img.Save(Path.Combine(folder, "new測試2.png"));
            img.Dispose();
        }
        /// <summary>
        /// 方法三
        /// 調用此函數后使此兩種圖片合並,類似相冊,有個
        /// 背景圖,中間貼自己的目標圖片
        /// </summary>
        /// <param name="sourceImg">粘貼的源圖片</param>
        /// <param name="destImg">粘貼的目標圖片</param>
        static private void CombinImage3()
        {
            const string folder = @"F:\測試圖片";
            Image img1 = Image.FromFile(Path.Combine(folder, "測試1.png"));//相框圖片 
            Image img2 = Image.FromFile(Path.Combine(folder, "測試2.png")); //照片圖片               
            //從指定的System.Drawing.Image創建新的System.Drawing.Graphics       
            Graphics g = Graphics.FromImage(img1);
            g.DrawImage(img1, 0, 0, 148, 124);// 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, 17, 17, 112, 73);
            GC.Collect();
            img1.Save(Path.Combine(folder, "new測試3.png"));
            img1.Dispose();
        }
    }
}

 


免責聲明!

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



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