C# 将多个Image 合成为一个,格式可选择

在一些场景中,需要把多个图片,合(拼)成为一张 ,合成效果示例:

纵向:

横向:

 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);
        }
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值