图片合并

    技术2022-07-10  143

    图片合并

    //两张图左右合并 static public void LRImageMerge(Image Img1, Image Img2) { int imgHeight, imgWidth = Img1.Width; imgWidth += Img2.Width; imgHeight = Img1.Height > Img2.Height ? Img1.Height : Img2.Height; Bitmap joinedBitmap = new Bitmap(imgWidth, imgHeight); Graphics graph = Graphics.FromImage(joinedBitmap); graph.DrawImage(Img1, 0, 0, Img1.Width, Img1.Height); graph.DrawImage(Img2, Img1.Width, 0, Img2.Width, Img2.Height); Image img = joinedBitmap; //保存 SaveFileDialog SaveFileDialog1 = new SaveFileDialog { Title = "图片另存为", Filter = "jpg图片|*.JPG|gif图片|*.GIF|png图片|*.PNG|jpeg图片|*.JPEG", FilterIndex = 3,//设置默认文件类型显示顺序 RestoreDirectory = true }; if (img != null) { if (SaveFileDialog1.ShowDialog() == DialogResult.OK) { string pictureName = SaveFileDialog1.FileName; //照片另存 using (MemoryStream mem = new MemoryStream()) { //这句很重要,不然不能正确保存图片或出错(关键就这一句) Bitmap bmp = new Bitmap(img); //保存到磁盘文件 bmp.Save(pictureName, img.RawFormat); bmp.Dispose(); MessageBox.Show("图片保存成功!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else { MessageBox.Show("没有图片信息!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information); } img.Dispose(); } //两张图上下合并 static public void UDImageMerge(Image Img1, Image Img2) { int imgHeight = Img1.Height, imgWidth; imgHeight += Img2.Height; imgWidth = Img1.Width > Img2.Width ? Img1.Width : Img2.Width; Bitmap joinedBitmap = new Bitmap(imgWidth, imgHeight); Graphics graph = Graphics.FromImage(joinedBitmap); graph.DrawImage(Img1, 0, 0, Img1.Width, Img1.Height); graph.DrawImage(Img2, 0, Img1.Height, Img2.Width, Img2.Height); Image img = joinedBitmap; //保存 SaveFileDialog SaveFileDialog1 = new SaveFileDialog { Title = "图片另存为", Filter = "jpg图片|*.JPG|gif图片|*.GIF|png图片|*.PNG|jpeg图片|*.JPEG", FilterIndex = 3,//设置默认文件类型显示顺序 RestoreDirectory = true }; if (img != null) { if (SaveFileDialog1.ShowDialog() == DialogResult.OK) { string pictureName = SaveFileDialog1.FileName; //照片另存 using (MemoryStream mem = new MemoryStream()) { //这句很重要,不然不能正确保存图片或出错(关键就这一句) Bitmap bmp = new Bitmap(img); //保存到磁盘文件 bmp.Save(pictureName, img.RawFormat); bmp.Dispose(); MessageBox.Show("图片保存成功!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else { MessageBox.Show("没有图片信息!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information); } img.Dispose(); } //四图合并 static public void ImageMerge(List<Image> Img) { int imgHeight = Img[0].Height, imgWidth = Img[0].Width; for(int i = 1; i< 4; i++) { imgWidth = imgWidth > Img[i].Width ? imgWidth : Img[i].Width; imgHeight = imgHeight > Img[i].Height ? imgHeight : Img[i].Height; } Bitmap joinedBitmap = new Bitmap(imgWidth<<1, imgHeight<<1); Graphics graph = Graphics.FromImage(joinedBitmap); graph.DrawImage(Img[0], 0, 0, Img[0].Width, Img[0].Height); graph.DrawImage(Img[1], imgWidth, 0, Img[1].Width, Img[1].Height); graph.DrawImage(Img[2], 0, imgHeight, Img[2].Width, Img[2].Height); graph.DrawImage(Img[3], imgWidth, imgHeight, Img[3].Width, Img[3].Height); Image img = joinedBitmap; //保存 SaveFileDialog SaveFileDialog1 = new SaveFileDialog { Title = "图片另存为", Filter = "jpg图片|*.JPG|gif图片|*.GIF|png图片|*.PNG|jpeg图片|*.JPEG", FilterIndex = 3,//设置默认文件类型显示顺序 RestoreDirectory = true }; if (img != null) { if (SaveFileDialog1.ShowDialog() == DialogResult.OK) { string pictureName = SaveFileDialog1.FileName; //照片另存 using (MemoryStream mem = new MemoryStream()) { //这句很重要,不然不能正确保存图片或出错(关键就这一句) Bitmap bmp = new Bitmap(img); //保存到磁盘文件 bmp.Save(pictureName, img.RawFormat); bmp.Dispose(); MessageBox.Show("图片保存成功!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else { MessageBox.Show("没有图片信息!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information); } img.Dispose(); }
    Processed: 0.009, SQL: 9