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

后端 未结 6 988
庸人自扰
庸人自扰 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

            String jpg1 = @"c:\images.jpeg";
            String jpg2 = @"c:\images2.jpeg";
            String jpg3 = @"c:\image3.jpg";
    
            Image img1 = Image.FromFile(jpg1);
            Image img2 = Image.FromFile(jpg2);
    
            int width = img1.Width + img2.Width;
            int height = Math.Max(img1.Height, img2.Height);
    
            Bitmap img3 = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(img3);
    
            g.Clear(Color.Black);
            g.DrawImage(img1, new Point(0, 0));
            g.DrawImage(img2, new Point(img1.Width, 0));
    
            g.Dispose();
            img1.Dispose();
            img2.Dispose();
    
            img3.Save(jpg3, System.Drawing.Imaging.ImageFormat.Jpeg);
            img3.Dispose();
    

提交回复
热议问题