加入图像函数
private Image JoinImage(List<Image> imageList, JoinMode jm) { //图片列表 if (imageList.Count <= 0) return null; if (jm == JoinMode.Horizontal) { //横向拼接 int width = 0; //计算总长度 foreach (Image i in imageList) { width += i.Width; } //高度不变 int height = imageList.Max(x => x.Height); //构造最终的图片白板 Bitmap tableChartImage = new Bitmap(width, height); Graphics graph = Graphics.FromImage(tableChartImage); //初始化这个大图 graph.DrawImage(tableChartImage, width, height); //初始化当前宽 int currentWidth = 0; foreach (Image i in imageList) { //拼图 graph.DrawImage(i, currentWidth, 0); //拼接改图后,当前宽度 currentWidth += i.Width; } return tableChartImage; } else if (jm == JoinMode.Vertical) { //纵向拼接 int height = 0; //计算总长度 foreach (Image i in imageList) { height += i.Height; } //宽度不变 int width = imageList.Max(x => x.Width); //构造最终的图片白板 Bitmap tableChartImage = new Bitmap(width, height); Graphics graph = Graphics.FromImage(tableChartImage); //初始化这个大图 graph.DrawImage(tableChartImage, width, height); //初始化当前宽 int currentHeight = 0; foreach (Image i in imageList) { //拼图 graph.DrawImage(i, 0, currentHeight); //拼接改图后,当前宽度 currentHeight += i.Height; } return tableChartImage; } else { return null; } }
拼接测试(button按钮事件下)
Bitmap Image1 = new Bitmap(Application.StartupPath + "\\未标题-2_01.bmp"); Bitmap Image2 = new Bitmap(Application.StartupPath + "\\未标题-2_02.bmp"); //pictureBox2.Image = Image1; //pictureBox3.Image = Image2; System.Windows.Forms.PictureBox aa = new PictureBox(); aa.Image = Image1; Image img1=aa.Image; aa.Image = Image2; Image img2 = aa.Image; int uniteWidth = img1.Width;//图片统一高度 int uniteHeight = img1.Height;//图片统一宽度 Bitmap tableChartImageCol1 = new Bitmap(uniteWidth * 2, uniteHeight);//第一行图片 List<Image> imageList = new List<Image>(); List<Image> imgListCopy = new List<Image>(); imageList.Add(img1); imageList.Add(img2); foreach (Image i in imageList)//笨方法,重新new图片,改变图片高度宽度 { Bitmap b = new Bitmap(i, uniteWidth, uniteHeight); imgListCopy.Add(b); } for (int i = 0; i < 2; i++) { if (imageList.Count - 1 >= i) { imageList[i] = imgListCopy[i];//更改后的图片,赋给原图片 } } if (true)//进去拼图 //横向拼接 { //图片白板1 Graphics graph = Graphics.FromImage(tableChartImageCol1); //初始化这个大图 graph.DrawImage(tableChartImageCol1, uniteWidth * 2, uniteHeight); //初始化当前宽 int currentWidth = 0; graph.Clear(System.Drawing.Color.White); ////清除画布,背景设置为白色 foreach (Image i in imageList) { graph.DrawImage(i, currentWidth, 0);//拼图--图片拼起来 currentWidth += i.Width; //拼接改图后,当前宽度 } } pictureBox1.Image = tableChartImageCol1;
效果图一,待拼接图片
拼接完成效果
时间: 2024-11-16 22:52:04