tags:

views:

118

answers:

1

Using GDI+ I've made a heatmap bmp and I'd like to superimpose it on top of my bmp map. I've saved the two bmps to disk, and they look good, I just need a way to put them together. Is there any way to do this, perhaps using the Graphics object? How is transparency/alpa involved?

I'm very new to GDI programming so please be as specific as possible.


OK - here's an answer. At some point I need to learn how GDI+ works...

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);     
+1  A: 

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


        }
MarkB29
This is the right idea. I called g.Clear(Color.Transparent) before drawing the imposed image, but the colors don't mix right. If there is anything in the second image I want it to completely obscure the first.
Old Man