Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two images to create a single image in C#.Net

I have a requirement wherein I need to merge two different png/jpeg images resulting into a single image using C#.Net. There will be a particular location defined on the source image wherein I need to insert another image. Can anybody suggest some links ?

like image 231
Anil Avatar asked Jun 17 '11 08:06

Anil


People also ask

Can you merge two images?

Open the Photo Gallery and locate the folder that contains photos you want to combine. Hold CTRL key to select multiple images and then click on the Photo Gallery's Create tab. Select the Photo Fuse feature and proceed to designate the area of the photo you want to replace.

How do I combine two picture codes?

Merging two imagesCreate an empty image using the Image. new() function. Paste the images using the paste() function. Save and display the resultant image using the save() and show() functions.


1 Answers

This method merge two images one in the top of the other you can modify the code to meet for your needs:

    public static Bitmap MergeTwoImages(Image firstImage, Image secondImage)     {         if (firstImage == null)         {             throw new ArgumentNullException("firstImage");         }          if (secondImage == null)         {             throw new ArgumentNullException("secondImage");         }          int outputImageWidth = firstImage.Width > secondImage.Width ? firstImage.Width : secondImage.Width;          int outputImageHeight = firstImage.Height + secondImage.Height + 1;          Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);          using (Graphics graphics = Graphics.FromImage(outputImage))         {             graphics.DrawImage(firstImage, new Rectangle(new Point(), firstImage.Size),                 new Rectangle(new Point(), firstImage.Size), GraphicsUnit.Pixel);             graphics.DrawImage(secondImage, new Rectangle(new Point(0, firstImage.Height + 1), secondImage.Size),                 new Rectangle(new Point(), secondImage.Size), GraphicsUnit.Pixel);         }          return outputImage;     } 
like image 193
Jalal Said Avatar answered Sep 21 '22 15:09

Jalal Said