C# – How to merge two icons together? (overlay one icon on top of another)


I've got two 16×16 RGB/A .ICO icon files, each loaded into a separate System.Drawing.Icon object.

How would you create a new Icon object containing the merge of the two icons (one overlaid on top of the other)?

I probably wasn't too clear, I don't want to blend two images into each other, I want to overlay one icon on top of another.

I should add that the icons already contain transparent parts and I do not need any transparent "blending" to make both icons visible. What I need is to overlay the non-transparent pixels of one icon over the top of another icon. The transparent pixels should let the background icon show through.

For example, look at the stackoverflow icon. It has some areas that are grey and orange, and some areas that are totally transparent. Imagine you want to overlay the SO icon on top of the Firefox icon. You would see the greys and oranges of the SO icon in full colour, and where the SO icon is transparent, you would see those parts of the Firefox icon.

Best Solution

Here's the final function I came up with. It was simpler than I thought...
Thanks to Eoin Campbell for doing the hard work.

public Icon AddIconOverlay(Icon originalIcon, Icon overlay)
    Image a = originalIcon.ToBitmap();
    Image b = overlay.ToBitmap();
    Bitmap bitmap = new Bitmap(16, 16);
    Graphics canvas = Graphics.FromImage(bitmap);
    canvas.DrawImage(a, new Point(0, 0));
    canvas.DrawImage(b, new Point(0, 0));
    return Icon.FromHandle(bitmap.GetHicon());