图片的合并
/// <summary>
/// 图片合并
/// </summary>
/// <param name="list">图片路径列表</param>
/// <param name="row">行数</param>
/// <param name="cloumn">列数</param>
/// <returns>生成的图片的文件名/错误消息</returns>
public static string MergeImage(List<string> list, int row, int cloumn, int width, int height)
{
if (list.Count != row * cloumn)
return "行列乘积不等于列表总数!";//不符合标准
Bitmap[] Capture = new Bitmap[list.Count]; // 要合并的图片的集合
int ImageWidth = 0;
int ImageHight = 0;
int maxHeight = 0;
int maxWidth = 0;
///控制图片的行宽度
for (int i = 0; i < list.Count; i++)
{
try
{
Capture[i] = new Bitmap(list[i].ToString());
}
catch (Exception e)
{
return "列表中图片" + i + "转换异常";
}
if (Capture[i].Height > ImageHight)
{
ImageHight = Capture[i].Height;
}
if (Capture[i].Width > ImageWidth)
{
ImageWidth = Capture[i].Width;
}
}
///获取图片列表中的最高值,和最宽值
maxWidth = ImageWidth;
maxHeight = ImageHight;
ImageWidth = ImageWidth * cloumn;
ImageHight = ImageHight * row;
Bitmap x = new Bitmap(ImageWidth, ImageHight);
Graphics g = Graphics.FromImage(x);
int tempX = 0;
int tempY = 0;
int bb = 0;
for (int a = 0; a < row; a++)
{
for (int b = 0; b < cloumn; b++)
{
///画图片
g.DrawImage(Capture[bb], tempX, tempY, Capture[bb].Width, Capture[bb].Height);
//g.DrawImage(Capture[bb], tempX, tempY, width, height);
tempX = tempX + maxWidth;
bb++;//前提条件是a*b=Capture.Length
}
tempX = 0;
tempY = tempY + maxHeight;//由于这里的每个图片的高度都是固定的,所以随便取的图片的高度
}
string imgName = DateTime.Now.ToFileTimeUtc().ToString() + ".jpg";
//目录若不存在,则创建
if (!Directory.Exists(MWHelper.mergeImg_path))
Directory.CreateDirectory(MWHelper.mergeImg_path);
string path = MWHelper.mergeImg_path + imgName;//生成日志文件名/
try
{
x.Save(path);
}
catch (Exception e)
{
return "保存合并之后的图片时发生异常!";
}
return path;
}
/// <summary>
/// 图片合并
/// </summary>
/// <param name="list">图片列表</param>
/// <param name="row">行数</param>
/// <param name="cloumn">列数</param>
/// <param name="width">生成的图片的宽度</param>
/// <param name="height">生成图片的高度</param>
/// <param name="character">图片之间的间隙</param>
/// <returns></returns>
public static string MergeImage(List<string> list, int row, int cloumn, int width, int height, int character)
{
if (list.Count != row * cloumn)
return "行列乘积不等于列表总数!";//不符合标准
Bitmap[] Capture = new Bitmap[list.Count]; // 要合并的图片的集合
int maxHeight = 0;
int maxWidth = 0;
///控制图片的行宽度
for (int i = 0; i < list.Count; i++)
{
try
{
Capture[i] = new Bitmap(list[i].ToString());
}
catch (Exception e)
{
return "列表中图片" + i + "转换异常";
}
}
///获取图片列表中的最高值,和最宽值
maxWidth = (width - (cloumn - 1) * character) / cloumn;
maxHeight = (height - (row - 1) * character) / row;
Bitmap x = new Bitmap(width, height);
Graphics g = Graphics.FromImage(x);
int tempX = 0;
int tempY = 0;
int bb = 0;
for (int a = 0; a < row; a++)
{
for (int b = 0; b < cloumn; b++)
{
///画图片
g.DrawImage(Capture[bb], tempX, tempY, maxWidth, maxHeight);
tempX = tempX + maxWidth + character;
bb++;//前提条件是a*b=Capture.Length
}
tempX = 0;
tempY = tempY + maxHeight + character;//由于这里的每个图片的高度都是固定的,所以随便取的图片的高度
}
string imgName = DateTime.Now.ToFileTimeUtc().ToString() + ".jpg";
//目录若不存在,则创建
if (!Directory.Exists(MWHelper.mergeImg_path))
Directory.CreateDirectory(MWHelper.mergeImg_path);
string path = MWHelper.mergeImg_path + imgName;//生成日志文件名/
try
{
x.Save(path);
}
catch (Exception e)
{
return "保存合并之后的图片时发生异常!";
}
return path;
}