Suleman
Suleman

Reputation: 712

C# Merging 4x images create single image but using these special images does not work

I have coded such that 4x images will merge into single image, dimensions will add up so that the final image ensures it always has space to hold all 4 images.

// Determining width and height

int widthF = Math.Max((globeVar.width1 + globeVar.width2), (globeVar.width3 + globeVar.width4));
int heightF = Math.Max((globeVar.height1 + globeVar.height2), (globeVar.height3 + globeVar.height4));


//Getting drawing objects ready
Image img = new Bitmap(widthF, heightF);
Graphics drawing = Graphics.FromImage(img);

//paint the background to check where image is not merged
drawing.Clear(Color.Blue);

drawing.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

// Draw the image to the graphics to create the new image 
// Which will be used in the onpaint background                 

drawing.DrawImage(globeVar.image1, 0, 0);
drawing.DrawImage(globeVar.image2, (img.Width) / 2, 0);
drawing.DrawImage(globeVar.image3, 0, (img.Height) / 2);
drawing.DrawImage(globeVar.image4, (img.Width) / 2, (img.Height) / 2);
drawing.Save();

img.Save(@globeVar.savePath, ImageFormat.Png);                

drawing.Dispose();

Now code actually works for the following 4 images:

https://i.sstatic.net/NVGc3.jpg

But when I use these images,:

https://i.sstatic.net/gRLTp.jpg

Merging becomes like this:

https://i.sstatic.net/ArQVA.jpg

What exactly am I missing ?

Edit:

Code I use for selecting image:

 //Getting The Image From The System
        OpenFileDialog open = new OpenFileDialog();
        open.Filter =
          "Image Files(*.jpg; *.jpeg; *.gif; *.png; *.bmp)|*.jpg; *.jpeg; *.gif; *.png; *.bmp";

        if (open.ShowDialog() == DialogResult.OK)
        {

            Bitmap img = new Bitmap(open.FileName);

            globeVar.image4 = Image.FromFile(open.FileName);

            globeVar.width4 = img.Width;
            globeVar.height4 = img.Height;


        }

This is the code that I use for global variables which helps me in using them anywhere I'd like in the project:

 public static class globeVar
{
    public static int width1 = 0;
    public static int width2 = 0;
    public static int width3 = 0;
    public static int width4 = 0;
    public static int height1 = 0;
    public static int height2 = 0;
    public static int height3 = 0;
    public static int height4 = 0;
    public static Image image1;
    public static Image image2;
    public static Image image3;
    public static Image image4;
    public static string savePath = "C:\\Users\\Suleman\\Desktop\\ImageMerge\\Sample Images\\test.png";
    //public static string savePath = "test.png";


}

Upvotes: 0

Views: 75

Answers (1)

Suleman
Suleman

Reputation: 712

I found the answer myself. It looks like that the DrawImage method was not considering the default size of image. So I had to define their original size in order for successful merging.

I just updated:

 drawing.DrawImage(globeVar.image1, 0, 0, globeVar.width1, globeVar.height1);
 drawing.DrawImage(globeVar.image2, (img.Width) / 2, 0, globeVar.width2, globeVar.height2);
 drawing.DrawImage(globeVar.image3, 0, (img.Height) / 2, globeVar.width3, globeVar.height3);
 drawing.DrawImage(globeVar.image4, (img.Width) / 2, (img.Height) / 2, globeVar.width4, globeVar.height4);

From docs:

I used before: DrawImage(Image, Int32, Int32)

I used now: DrawImage(Image, Int32, Int32, Int32, Int32)

Upvotes: 1

Related Questions