如何将两幅图像合并为一幅图像,两幅图像组合成一幅透明覆盖另一幅图像?

How can merge two images to be one image with the two images combined one transparent over the second one?

Bitmap bmp1 = new Bitmap(@"c:\coneimages\Cone_Images1.gif");
Bitmap bmp2 = new Bitmap(@"c:\coneimages\PictureBox_Images1.gif");
Bitmap bmp3 = new Bitmap(MergeTwoImages(bmp1,bmp2));
bmp3.Save(@"c:\coneimages\merged.bmp");

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

但这不是我想要的,我不确定如何以及在 google 中寻找什么。 我想要的是 bmp1 将覆盖 bmp2 之类的层。 bmp1 是透明的,我希望它像 bmp2 上的层一样。

所以在 bmp3 中我会看到带有 bmp1 的整个常规 bmp2。

假设 bmp1 中有 alpha,先绘制 bmp2 ,然后将合成模式设置为 SourceOver(默认)绘制bmp1。这应该完成正确的 alpha 混合顺序。

换句话说...

Bitmap bmp3 = new Bitmap(MergeTwoImages(bmp2,bmp1)); // Swapped arguments.

如果bmp1不包含 alpha,您将需要使用颜色矩阵来更改透明度。

Bitmap m_back = new Bitmap(bmp2.Width, bmp2.Height);
for (int y = 0; y < bmp2.Height; y++)
{
     for (int x = 0; x < bmp2.Width; x++)
     {
          Color temp = Color.FromArgb(80, bmp2.GetPixel(x, y));
          m_back.SetPixel(x, y, temp);
     }
}

Bitmap bmp3 = new Bitmap(MergeTwoImages(bmp1, m_back));