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

横向:

代码实现:
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();
}
代码说明:
PHP经典实例(第2版)能够为您节省宝贵的Web开发时间。有了这些针对真实问题的解决方案放在手边,大多数编程难题都会迎刃而解。《PHP经典实例(第2版)》将PHP的特性与经典实例丛书的独特形式组合到一起,足以帮您成功地构建跨浏览器的Web应用程序。在这个修订版中,您可以更加方便地找到各种编程问题的解决方案,《PHP经典实例(第2版)》中内容涵盖了:表单处理;Session管理;数据库交互;使用We
453
根据参数进行横向或纵向合并图片
如果为横向,图片高度为最高的那张;如果纵向则宽度为最宽的那张
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)!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号