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

后端 未结 6 1030
庸人自扰
庸人自扰 2020-12-13 14:00

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 sou

6条回答
  •  余生分开走
    2020-12-13 14:32

    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;
        }
    

提交回复
热议问题