如何在GDI+中将一个位图图像叠加在另一个图像上?

4
使用GDI+,我制作了一个热力图bmp,并希望将其叠加在我的bmp地图上。我已将这两个bmp保存到磁盘中,并且它们看起来很好,我只需要一种方法将它们结合起来。是否有任何方法可以做到这一点,也许使用Graphics对象?透明度/阿尔法如何涉及其中?
我对GDI编程非常陌生,请尽可能具体。
好的 - 这是一个答案。总有一天我需要学习如何使用GDI+...
我无法解决透明度问题,但是这个方法有效。它只是从覆盖层复制非白色像素到地图中:
        for (int x = 0; x < map.Width; x++)
            for (int y = 0; y < map.Height; y++) {
                Color c = overlay.GetPixel(x, y);
                if ((c.A != 255) || (c.B != 255) || (c.G != 255) || (c.R != 255))
                    map.SetPixel(x, y, c);     

可能需要添加Windows Forms标签,因为它是包装GDI+的.NET库。 - Steve Dennis
顺便说一句,在http://www.vbforums.com/showthread.php?t=565995上有一个类似的帖子。 - James Manning
1个回答

7
这应该能满足你的需求...
目前,你想要叠加到主图片上的图像将位于主图片的左上角,因此使用了 new Point(0,0)。不过,你可以根据需要将图像定位在任何位置。
void SuperimposeImage()
        {
            //load both images
            Image mainImage = Bitmap.FromFile("PathOfImageGoesHere");
            Image imposeImage = Bitmap.FromFile("PathOfImageGoesHere");

            //create graphics from main image
            using (Graphics g = Graphics.FromImage(mainImage))
            {
                //draw other image on top of main Image
                g.DrawImage(imposeImage, new Point(0, 0));

                //save new image
                mainImage.Save("OutputFileName");
            }


        }

这是正确的想法。在绘制叠加图像之前,我调用了g.Clear(Color.Transparent),但颜色混合得不对。如果第二个图像中有任何东西,我希望它完全遮盖第一个图像。 - Old Man

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接