C# 將多個Image 合成為一個,格式可選擇

來源:互聯網
上載者:User
在一些情境中,需要把多個圖片,合(拼)成為一張 ,合成效果樣本:

縱向:


橫向:


代碼實現:

 enum ImageMergeOrientation    {        Horizontal,        Vertical    }
private void CombineImages(FileInfo[] files, string toPath, ImageMergeOrientation mergeType = ImageMergeOrientation.Vertical)        {            //change the location to store the final image.            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);        }


以上就是C# 將多個Image 合成為一個,格式可選擇的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.