需要关注的是第二张图片需要透明,这样才可以保持两张Bitmap图片各自的特点,具体代码如下:
public static Bitmap CombineImages()
{
//图片路径自己定义 格式如下
Bitmap image1 = new Bitmap("D\\TempDir\\img1.png");
Bitmap image2 = new Bitmap("D:\\TempDir\\img2.png");
int width = Math.Max(image1.Width, image2.Width);
int height = Math.Max(image1.Height, image2.Height);
//因为底色是黑色的 所以设置黑色透明 可以根据具体情况自行设置
image2.MakeTransparent(System.Drawing.Color.Black);
image2.Save(System.IO.Path.Combine(@"D:\TempDir\", "image3" + ".png"));
Bitmap resultImage = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(resultImage))
{
graphics.DrawImage(image1, 0, 0);
graphics.DrawImage(image2, 0, 0);
}
resultImage.Save(System.IO.Path.Combine(@"D:\TempDir\", "new_" + ".png"));
return resultImage;
}
以上代码就可以实现图片叠加生成一张新的图片。