C#把多个图片合并成一张图片。
private void button7_Click(object sender, EventArgs e) { //把2个图片合并到一个图片中 //文件路径 string fs = Application.StartupPath +"\\PIC\\"; //不存在文件进行创建 if (!System.IO.Directory.Exists(fs)) System.IO.Directory.CreateDirectory(fs); //存储到pic文件夹中文件 ocx.PadFileSaveAs(fs, 22, 1); //获取到pic下面的jpg图片 string[] rs = System.IO.Directory.GetFiles(fs, "*.jpg"); //最大宽度和高度 int maL = 0, totalH = 0; //循环遍历获取文件的最大宽度与总高度 for (int i = 0; i < rs.Length; i++) { Image image = Image.FromStream(new System.IO.MemoryStream(File.ReadAllBytes(rs[i]))); if (image.Width > maL) maL = image.Width; totalH = totalH + image.Height + 5; } if (totalH == 0 || maL == 0) return; Bitmap map = new Bitmap(maL, totalH);//定义画布 Graphics g = Graphics.FromImage(map);//定义画笔 g.Clear(Color.White);//把画布更改为白色 int y=0;//y轴坐标 for (int i = 0; i < rs.Length; i++) { Image image = Image.FromStream(new System.IO.MemoryStream(File.ReadAllBytes(rs[i]))); g.DrawImage(image, new Point(0, y)); y = y + image.Height + 5;//y的告诉 5是为了让画布有个缝隙 } //把合并的图片进行保存为jpg格式 map.Save(Application.StartupPath + "\\1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); for (int i = 0; i < rs.Length; i++) { //删除原先的2个jpg图片 File.Delete(rs[i]); } }
效果图: