0

I am developing a c# windows form application.. I want to merge two picturebox and save it to custom file location.. I used following code to do it. But it only save 2nd picturebox image. Could anybody tell me how to merge this two picturebox and save it..

        {
            // open file dialog   
            OpenFileDialog open = new OpenFileDialog();
            // image filters  
            open.Filter = "Image Files(*.jpg; *.jpeg; *.gif;*.png; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp;*.png";
            if (open.ShowDialog() == DialogResult.OK)
            {
                // display image in picture box  
                pictureBox2.Image = new Bitmap(open.FileName);
                //combine two images
                
                // image file path  
                textBox1.Text = open.FileName;

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "JPG(*.JPG)|*.jpg";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                previewimg.Image.Save(dialog.FileName);
                pictureBox2.Image.Save(dialog.FileName);
            }
        }
    }
  • 1
    This is overwriting first file by the second file, not merging them. – Tanveer Badar Jul 18 '22 at 17:58
  • yea. i understood it. I don't know how to merge this.. – Supun Madhushanka Jul 18 '22 at 17:58
  • You can do it with ImageSharp https://stackoverflow.com/a/50886285/7357322 – Andrii Khomiak Jul 18 '22 at 17:59
  • 2
    Picture boxes are used to display images, they don't do any image modification. You need to look and [image processing](https://devblogs.microsoft.com/dotnet/net-core-image-processing/). – quaabaam Jul 18 '22 at 18:02
  • 1
    What does "merge" mean? – NetMage Jul 18 '22 at 18:04
  • @NetMage I want to combine two images which are locating on separate picturebox to one single image, i meant to a new picturebox – Supun Madhushanka Jul 18 '22 at 18:08
  • No. You want to combine two images/Bitmaps. You can then display the result in a pbox. Best use DrawImage twice with a graphics coming from a new bitmap, that is large enough. - You could also put the pboxes into a Panel and use panel.DrawToBitmap. – TaW Jul 18 '22 at 21:34
  • "combine two images" - what does that mean? There are millions of ways to combine two images - which one did you want? – NetMage Jul 19 '22 at 19:08

1 Answers1

1

Maybe need some adjustements (not tested) but I think it's useful:

using (var src1 = new Bitmap(@"c:\...\image1.png"))
using (var src2 = new Bitmap(@"c:\...\image2.png"))
using (var bmp = new Bitmap(src1.Width + src2.Width, src1.Height, PixelFormat.Format32bppPArgb))
using (var g = Graphics.FromImage(bmp))
{
    g.Clear(Color.Black);
    g.DrawImage(src1, new Rectangle(0, 0, src1.Width, src1.Height));
    g.DrawImage(src2, new Rectangle(src1.Width, 0, src2.Width, src2.Height));
    bmp.Save(@"c:\...\combined.png", ImageFormat.Png);
}

UPDATE

private void button2_Click(object sender, EventArgs e)
{
    SaveFileDialog dialog = new SaveFileDialog();
    dialog.Filter = "JPG(*.JPG)|*.jpg";
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        var src1 = previewimg.Image;
        var src2 = pictureBox2.Image;

        using (var bmp = new Bitmap(src1.Width + src2.Width, src1.Height, PixelFormat.Format32bppPArgb))
        using (var g = Graphics.FromImage(bmp))
        {
            g.Clear(Color.Black);
            g.DrawImage(src1, new Rectangle(0, 0, src1.Width, src1.Height));
            g.DrawImage(src2, new Rectangle(src1.Width, 0, src2.Width, src2.Height));
            bmp.Save(@"c:\...\combined.png", ImageFormat.Png);
        }
    }
}
Victor
  • 2,313
  • 2
  • 5
  • 13