How can I merge two images side by side with option to add/remove space between the images ?

sharon glipman 441 Reputation points
2021-11-07T23:56:04.773+00:00

This method merge the two images side by side but not yet perfect as I wanted.

private Bitmap MergeImages(Image image1, Image image2)  
        {  
            Bitmap bitmap = new Bitmap(image1.Width + image2.Width, Math.Max(image1.Height, image2.Height));  
            using (Graphics g = Graphics.FromImage(bitmap))  
            {  
                g.DrawImage(image1, 0, 0);  
                g.DrawImage(image2, image1.Width, 0);  
            }  
  
            return bitmap;  
        }  

using it

MergeImages(Image.FromFile(filesRadar[filesRadar.Length - 1]),  
                                Image.FromFile(filesSatellite[filesSatellite.Length - 1])).Save(@"d:\Downloaded Images\merged.bmp");  

The result

147116-merged.jpg

The problem is on the left image there is a bit white area and in the original image it's not exist so how can I make that the white area will not be exist ?

The second problem is how to add a variable for the space between the images ? space I mean not white area between them but just a space between them
a variable for example if I set the variable value to 0 the merged image will be as it is now in the screenshot without space but if I set the value to 10 for example then there will be a space of 10 units(not sure what units) space.

because is hard to see the white area at the bottom of the left image I took a screenshot of my screen to show it more clear :

147146-merged1.jpg

The left image original is like this without the white area at the bottom and this is how I want it to be when merging both images :

147134-radarimage34.jpg

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,238 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2021-11-11T08:52:56.903+00:00

    @sharon glipman , I make some changes on your code and it could add space between two images.
    My idea is that we could add the width when we merge the second image.

    Code:

     private Bitmap MergeImages(Image image1, Image image2,int space)  
            {  
                Bitmap bitmap = new Bitmap(image1.Width + image2.Width+space, Math.Max(image1.Height, image2.Height));  
                using (Graphics g = Graphics.FromImage(bitmap))  
                {  
                    g.Clear(Color.Black);  
                    g.DrawImage(image1, 0, 0);  
                    g.DrawImage(image2, image1.Width+space, 0);  
                }  
                Image img = bitmap;  
                  
                img.Save("D:\\3.jpg");  
                return bitmap;  
            }  
    
    
    Bitmap b = MergeImages(img1, img2,10);  
    

    In order to test the result, I used the same picture and set the picture background color is black.

    Result:

    148532-3.jpg


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments