Diego Moreno
Diego Moreno

Reputation: 131

Create one image from multiple images in C#

I need to combine multiple images into one image. This I managed to do. However, I need these images to have specific positions within the final image. To do this, I need to use Bitmap with Graphics, however, I am a little bit confused on how to do this.

For Example:

I have 4 images files (img1.png, img2.png, img3.png, img4.png), I need to create one single image with these four images in it at the specified pixel coordinates:

  1. img1.png dimensions: top=20px, left=30px, width: 70px, height: 50px
  2. img2.png dimensions: top=70px, left=80px, width: 50px, height: 30px
  3. img3.png dimensions: top=120px, left=30px, width: 110px, height: 80px
  4. img4.png dimensions: top=12px, left=200px, width: 70px, height: 90px

Upvotes: 1

Views: 7069

Answers (1)

MD. Nazmul Kibria
MD. Nazmul Kibria

Reputation: 1090

First create an image using the total width & height. Need to calculate the width & height from the images which are going to stitch on a single image. Now use below code to stitch:

using (Bitmap bmp = new Bitmap(cal_width, cal_height))
{
    using (Graphics g = Graphics.FromImage(bmp))
    {
        g.DrawImage(img1,x1,y1,w1,h1);
        g.DrawImage(img2, x2, y2, w2, h2);
        g.DrawImage(img3, x3, y3, w3, h3);
        g.DrawImage(img4, x4, y4, w4, h4);
    }
}

bmp will the desired image. x1,y1...x4,y4 are the positions of the top left point from where you start place the image. These positions need to calculate in respective to the final image. Also place the width & height of the images using w1,h1...w4,h4.

Upvotes: 3

Related Questions