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

黄舟
发布: 2017-03-01 10:48:35
原创
2274 人浏览过

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

纵向:


横向:


代码实现:

 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 合成为一个,格式可选择的内容,更多相关内容请关注PHP中文网(www.php.cn)!


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!