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

时间:2010-06-03 13:22:03

标签: c# gdi+ heatmap

使用GDI +我已经制作了热图bmp,我想将它叠加在我的bmp地图上。我已经将两个bmps保存到磁盘上,它们看起来很好,我只需要一种方法将它们组合在一起。有没有办法做到这一点,也许使用Graphics对象?如何涉及透明度/ alpa?

我对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);     

1 个答案:

答案 0 :(得分: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");
            }


        }