纵向:
横向:
代码实现:
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 合成为一个,格式可选择的内容。