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

sharon glipman 421 Reputation points

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


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 :


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 :


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.
7,020 questions
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,329 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 19,381 Reputation points Microsoft Employee

    @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.


     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.DrawImage(image1, 0, 0);  
                    g.DrawImage(image2, image1.Width+space, 0);  
                Image img = bitmap;  
                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.



    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.

    No comments