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)!