C#把多個圖片合並成一張圖片。


  private void button7_Click(object sender, EventArgs e)
        {
           //把2個圖片合並到一個圖片中
            //文件路徑
            string fs = Application.StartupPath +"\\PIC\\";
            //不存在文件進行創建
            if (!System.IO.Directory.Exists(fs)) System.IO.Directory.CreateDirectory(fs);
            //存儲到pic文件夾中文件
            ocx.PadFileSaveAs(fs, 22, 1);
            //獲取到pic下面的jpg圖片
            string[] rs = System.IO.Directory.GetFiles(fs, "*.jpg");
            //最大寬度和高度
            int maL = 0, totalH = 0;

            //循環遍歷獲取文件的最大寬度與總高度
            for (int i = 0; i < rs.Length; i++)
            {
                Image image = Image.FromStream(new System.IO.MemoryStream(File.ReadAllBytes(rs[i])));
                if (image.Width > maL) maL = image.Width;
                totalH = totalH +  image.Height + 5;
            }
            if (totalH == 0 || maL == 0) return;
            Bitmap map = new Bitmap(maL, totalH);//定義畫布
            Graphics g = Graphics.FromImage(map);//定義畫筆
            g.Clear(Color.White);//把畫布更改為白色
            int y=0;//y軸坐標
            for (int i = 0; i < rs.Length; i++)
            {
                Image image = Image.FromStream(new System.IO.MemoryStream(File.ReadAllBytes(rs[i])));
                g.DrawImage(image, new Point(0, y));
                y = y + image.Height + 5;//y的告訴 5是為了讓畫布有個縫隙
            }
            //把合並的圖片進行保存為jpg格式
            map.Save(Application.StartupPath + "\\1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            for (int i = 0; i < rs.Length; i++)
            {
                //刪除原先的2個jpg圖片
                File.Delete(rs[i]);
            }

        }

效果圖:

 


免責聲明!

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



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