本次程序编写主要为了将pdf word等文档转换为图片后设置不同的打印排版
前提 目标文件夹中的图片高宽都是一致的
/// <summary>/// 合并图片/// </summary>/// <param name="savedictory">文件保存目录</param>/// <param name="singlePageNumber">单页排版</param>/// <param name="path">文件列表</param>/// public static void MergedImage(string[] path, SinglePageNumber singlePageNumber,string savedictory){//获取目录路径列表var i = path.Length % (int)singlePageNumber > 0 ? (path.Length / (int)singlePageNumber + 1) : path.Length / (int)singlePageNumber;for (int s = 0; s < i; s++){List<Bitmap> bitmapList = new List<Bitmap>();List<string> imgPathTake = path.Skip((s - 1) * (int)singlePageNumber).Take((int)singlePageNumber).ToList();foreach (var pathItem in imgPathTake)bitmapList.Add(new Bitmap(pathItem));//2=1x2 4=2x2 6=2x3 9=3x3 16=4x4Bitmap map = null;switch (singlePageNumber){case SinglePageNumber.two:map = mergeImage(2, 1, bitmapList);break;case SinglePageNumber.four:map = mergeImage(2, 2, bitmapList);break;case SinglePageNumber.six:map = mergeImage(3, 2, bitmapList);break;case SinglePageNumber.nine:map = mergeImage(3, 3, bitmapList);break;case SinglePageNumber.Sixteen:map = mergeImage(4, 4, bitmapList);break;default:break;}map.Save(savedictory + "\\" + s+".png");}}/// <summary>/// 合并图片文件 传入的图片宽高要一致/// </summary>/// <param name="wi">一排有几张</param>/// <param name="hi">一列有几张</param>/// <param name="mapList"></param>public static Bitmap mergeImage(int wi, int hi, List<Bitmap> mapList){int width = mapList.First().Width;int height = mapList.First().Height;Bitmap bitmap = new Bitmap(width * wi, height * hi);//x表示图片在第几行y表示图片在第几列 int mapIndex = 0;for (int y = 1; y <= hi; y++){for (int x = 1; x <= wi; x++){if (mapIndex == mapList.Count ){foreach (var item in mapList)item.Dispose();return bitmap;}var map = mapList[mapIndex];mapIndex++;for (int w = 1; w <= width; w++)for (int h = 1; h <= height; h++){var color = map.GetPixel(w - 1, h - 1);bitmap.SetPixel(((x-1)*width)+w - 1, ((y - 1) * height) + h - 1, color);}}}foreach (var item in mapList)item.Dispose();return bitmap;}
/// <summary>/// 每版打印几页 单页的不用处理/// </summary>public enum SinglePageNumber{two = 2,four = 4,six = 6,nine = 9,Sixteen = 16,}
设置图片排版为2*3的效果
string dictory = @"D:\test\testpdf2convert\";string[] path = System.IO.Directory.GetFiles(dictory);ImagePrintHp.MergedImage(path, SinglePageNumber.six, dictory);
效果
2*2效果