程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#中如何把多個圖片合成為一個圖片

C#中如何把多個圖片合成為一個圖片

編輯:關於C#

在一些場景中,需要把多個圖片,合(拼)成為一張 ,合成效果示例:

縱向:

橫向:

代碼實現:

enum ImageMergeOrientation  
   {  
       Horizontal,  
       Vertical  
   }
private void CombineImages(FileInfo[] files, string toPath, ImageMergeOrientation mergeType = ImageMergeOrientation.Vertical)  
        {  
            //change the location to store the final image.
			// URL:http://www.bianceng.cn/Programming/csharp/201410/45751.htm
            var finalImage = toPath;  
            var imgs = files.Select(f => Image.FromFile(f.FullName));  
       
            var finalWidth = mergeType == ImageMergeOrientation.Horizontal ?  
                imgs.Sum(img => img.Width) :  
                imgs.Max(img => img.Width);  
       
            var finalHeight = mergeType == ImageMergeOrientation.Vertical ?  
                imgs.Sum(img => img.Height) :  
                imgs.Max(img => img.Height);  
       
            var finalImg = new Bitmap(finalWidth, finalHeight);  
            Graphics g = Graphics.FromImage(finalImg);  
            g.Clear(SystemColors.AppWorkspace);  
       
            var width = finalWidth;  
            var height = finalHeight;  
            var nIndex = 0;  
            foreach (FileInfo file in files)  
            {  
                Image img = Image.FromFile(file.FullName);  
                if (nIndex == 0)  
                {  
                    g.DrawImage(img, new Point(0, 0));  
                    nIndex++;  
                    width = img.Width;  
                    height = img.Height;  
                }  
                else
                {  
                    switch (mergeType)  
                    {  
                        case ImageMergeOrientation.Horizontal:  
                            g.DrawImage(img, new Point(width, 0));  
                            width += img.Width;  
                            break;  
                        case ImageMergeOrientation.Vertical:  
                            g.DrawImage(img, new Point(0, height));  
                            height += img.Height;  
                            break;  
                        default:  
                            throw new ArgumentOutOfRangeException("mergeType");  
                    }  
                }  
                img.Dispose();  
            }  
            g.Dispose();  
            finalImg.Save(finalImage, System.Drawing.Imaging.ImageFormat.Tiff);  
            finalImg.Dispose();  
        }

代碼說明:

根據參數進行橫向或縱向合並圖片

如果為橫向,圖片高度為最高的那張;如果縱向則寬度為最寬的那張

UT 代碼:

[TestMethod]  
        public void Combine_Multiple_SampleImages_IntoOne()  
        {  
            const string folderPath = "C:\\Users\\Public\\Pictures\\Sample Pictures";
            var images = new DirectoryInfo(folderPath).GetFiles("*.jpg", SearchOption.TopDirectoryOnly);  
       
            CombineImages(images, "C:/FinalImage_H.tiff");  
            CombineImages(images, "C:/FinalImage_V.tiff", ImageMergeOrientation.Vertical);  
        }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved