图片合并

图片合并

//两张图左右合并
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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值