简体   繁体   English

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

[英]How do I superimpose one bitmap image on another in GDI+?

Using GDI+ I've made a heatmap bmp and I'd like to superimpose it on top of my bmp map. 使用GDI +我已经制作了热图bmp,我想将它叠加在我的bmp地图上。 I've saved the two bmps to disk, and they look good, I just need a way to put them together. 我已经将两个bmps保存到磁盘上,它们看起来很好,我只需要一种方法将它们组合在一起。 Is there any way to do this, perhaps using the Graphics object? 有没有办法做到这一点,也许使用Graphics对象? How is transparency/alpa involved? 如何涉及透明度/ alpa?

I'm very new to GDI programming so please be as specific as possible. 我对GDI编程很新,所以请尽可能具体。


OK - here's an answer. 好的 - 这是一个答案。 At some point I need to learn how GDI+ works... 在某些时候,我需要了解GDI +的工作原理......

I couldn't get around the transarency issues, but this works. 我无法解决透明度问题,但这有效。 It just copies the non-white pixels from the overlay to the map: 它只是将非白色像素从叠加层复制到地图:

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

This should do the job... 这应该做的工作......

At the moment the Image you want to superimpose onto the main image will be located in the top left corner of the main Image, hence the new Point(0,0) . 此时,您想要叠加到主图像上的图像将位于主图像的左上角,因此new Point(0,0) However you could change this to locate the image anywhere you want. 但是,您可以更改此设置以在任何位置找到图像。

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


        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM