C#은 선택 가능한 형식을 사용하여 여러 이미지를 하나로 결합합니다.

黄舟
풀어 주다: 2017-03-01 10:48:35
원래의
2273명이 탐색했습니다.

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

纵向:


横向:


代码实现:

 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 학습자의 빠른 성장을 도와주세요!