C# 将多个Image 合成为一个,格式可选择
2014-04-10 14:54
337 查看
在一些场景中,需要把多个图片,合(拼)成为一张 ,合成效果示例:
纵向:
横向:
代码实现:
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);
}
纵向:
横向:
代码实现:
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#中将byte[]数组转换为Image的问题
- 合并一个图层选择对象C# arcengine
- 如何去绘制一个圆并且转为imageView格式
- [C#] 如何选择一个目录
- C# 判断一个string型的时间格式是否正确
- C# String.Format的格式限定符与Format方法将多个对象格式化一个字符串原理
- C#中如何把串口接收的两个字节,合成一个short整型
- “System.BadImageFormatException”类型的未经处理的异常在 PurchaseDevices.Access.dll 中发生 其他信息: 未能加载文件或程序集“System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139”或它的某一个依赖项。试图加载格式不正确
- 解决eWebEditor上传图片提示:请选择一个有效的文件,支持的格式有(GIF|JPG|JPEG|BMP|PNG)!
- C# DataGridView 导出 Excel(根据Excel版本显示选择不同后缀格式xls或xlsx)
- 如何用c#打开文件夹并选择文件夹内的一个文件 open folder and select file
- C# Devexpress TreeListLookUpEdit控件默认选择一个数据
- 1、怎样设置C#OpenFileDialog(文件选择窗体)的指定路径、文件格式等属性(设置打开默认路径、文件格式、窗体显示文本)
- C# 合并多个不同格式文档为一个PDF
- C# 图片的裁剪,两个图片合成一个图片
- C#中如何把Sql Server中的image格式转换成图片
- C# 将多个DLL和exe合成一个exe程序
- C# Devexpress TreeListLookUpEdit控件默认选择一个数据
- C# 未能加载文件或程序集“XXX”或它的某一个依赖项。试图加载格式不正确的程序。
- 如何在iOS中使用ZXing库(ZXing是一个开源的条码生成和扫描库,开源协议为Apache2.0。它持众多条码格式和语言,比如Java、 C++、 C#、 Objective-C以及Act )