C # combine multiple image into one, format selectable

Source: Internet
Author: User
In some scenarios, you need to put multiple pictures, together (spell) into one, the composite effect example:

Longitudinal:


Transverse:


Code implementation:

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) {C                            ASE 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 (); }


Code Description:

Merge pictures horizontally or vertically based on parameters

If it is horizontal, the height of the picture is the highest one, and the width is the widest one if it is lengthwise.

UT Code:

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


The above is C # to make more than one image, the format can choose content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.