/// <summary>
/// 图片合并帮助类
/// </summary>
public static class MergeImageHelper
{
//下载图片
public static string DownloadImg(string strPath, string strName)
{
WebClient my = new WebClient();
byte[] mybyte;
mybyte = my.DownloadData(strPath);
MemoryStream ms = new MemoryStream(mybyte);
System.Drawing.Image img;
img = System.Drawing.Image.FromStream(ms);
string filePath = AppDomain.CurrentDomain.BaseDirectory + "imgUploads\\ActivtyImg\\" + strName;
img.Save(filePath, ImageFormat.Png); //保存
return filePath;
}
//调整图像大小
public static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
{
//获取图片宽度
int sourceWidth = imgToResize.Width;
//获取图片高度
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
//计算宽度的缩放比例
nPercentW = ((float)size.Width / (float)sourceWidth);
//计算高度的缩放比例
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
//期望的宽度
int destWidth = (int)(sourceWidth * nPercent);
//期望的高度
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((System.Drawing.Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//绘制图像
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
imgToResize.Dispose();
return (System.Drawing.Image)b;
}
//拼图函数
public static void MergeImage(List<MergeImg> mapLists, string saveFile, string desc)
{
if (mapLists == null && mapLists.Count <= 0)
{
return;
}
// 图片集合
Bitmap[] maps = new Bitmap[mapLists.Count];
for (int i = 0; i < mapLists.Count; i++)
{ //读取本地图片初始化Bitmap
Bitmap map = null;
//第一个图片对象,背景图片
map = new Bitmap(mapLists[i].Path);
maps[i] = map;
}
// 初始化背景图片的宽高
Bitmap bitMap = new Bitmap(mapLists[0].Width, mapLists[0].Height);
// 初始化画板
Graphics g1 = Graphics.FromImage(bitMap);
for (int k = 0; k < maps.Length; k++)
{
//绘制图片,背景图
for (int i = 0; i < maps[k].Width; i++)
{
for (int j = 0; j < maps[k].Height; j++)
{
// 以像素点形式绘制(将要拼图的图片上的每个坐标点绘制到拼图对象的指定位置,直至所有点都绘制完成)
var temp = maps[k].GetPixel(i, j);
//透明背景跳过
if (temp.A == 0 && temp.R == 0 && temp.G == 0 && temp.B == 0) { continue; }
// 将图片画布的点绘制到整体画布的指定位置
bitMap.SetPixel(mapLists[k].LeftWidth + i, mapLists[k].TopHeight + j, temp);
}
}
maps[k].Dispose();
}
//写入文字
for (int i = 0; i < (desc.Length / 10 + 1); i++)
{
int length = 10;
if (i == desc.Length / 10)
{
length = desc.Length - 10 * i;
}
if (length > 0)
{
g1.DrawString(desc.Substring(10 * i, length),
new Font(FontFamily.GenericSansSerif, 10f),
Brushes.Black, new PointF(20, 240 + i * 25)); //可自定义高度
}
}
// 保存输出到本地
bitMap.Save(saveFile, ImageFormat.Png);
g1.Dispose();
bitMap.Dispose();
}
}
/// <summary>
/// 合并图片类
/// </summary>
public class MergeImg
{
/// <summary>
/// 图片路径
/// </summary>
public string Path { get; set; }
/// <summary>
/// 左边距
/// </summary>
public int LeftWidth { get; set; }
/// <summary>
/// 上边距
/// </summary>
public int TopHeight { get; set; }
/// <summary>
/// 宽度
/// </summary>
public int Width { get; set; }
/// <summary>
/// 宽度
/// </summary>
public int Height { get; set; }
}