0

I'm at changing Icons Color, wich works so far, but now my Pixels are loosing their transparency, even while I actively try to prevent this. Here is my code:

private static void ChangeColor()
    {
        foreach(MultiIcon mi in listMi)
            foreach (SingleIcon si in mi)
                for (int n = 0; n < si.Count(); n++)
                {
                    IconImage ii = si[n];
                    Bitmap bmp = new Bitmap(ii.Image);
                    for (int w = 0; w < bmp.Width; w++)
                        for (int h = 0; h < bmp.Height; h++)
                            if (bmp.GetPixel(w, h).R == identifier - uncertanty && bmp.GetPixel(w, h).R == identifier + uncertanty)
                                bmp.SetPixel(w, h, Color.FromArgb(bmp.GetPixel(w, h).A, r, g, b));
                    ii.Set(bmp, null, Color.Transparent);
                }
    }

With this line, I take my original alpha-value and reuse it when setting my new (with changed color) pixel.

bmp.SetPixel(w, h, Color.FromArgb(bmp.GetPixel(w, h).A, r, g, b));

This is used for Saving the file. MultiIcon comes from IconLib:

http://www.codeproject.com/Articles/16178/IconLib-Icons-Unfolded-MultiIcon-and-Windows-Vista

mi.Save(NewPath(pathArray[i]), MultiIconFormat.ICO);

Another Issue is, that my Icon (but not the single icon-frames) are double as hight while still the same width.

EDIT:

I changed the PixelFormat to Format32bppPArgb which should be, what I need for transparecy, also made sure the Bitmap gets created Pixel by Pixel from the original, instead of copying and changing the it. Still, the same issue occures.

This is the new Code:

for (int n = 0; n < si.Count(); n++)
{
    IconImage ii = si[n];
    Bitmap bmp = new Bitmap(ii.Image.Height, ii.Image.Width, PixelFormat.Format32bppPArgb);
    for (int w = 0; w < bmp.Width; w++)
        for (int h = 0; h < bmp.Height; h++)
        {
            if (si[n].Image.GetPixel(w, h).R > identifier - uncertanty && si[n].Image.GetPixel(w, h).R < identifier + uncertanty)
                bmp.SetPixel(w, h, Color.FromArgb(si[n].Image.GetPixel(w, h).A, r, g, b));
            else
            {
                Color c = Color.FromArgb(       
                    si[n].Image.GetPixel(w, h).A, 
                    si[n].Image.GetPixel(w, h).R, 
                    si[n].Image.GetPixel(w, h).G, 
                    si[n].Image.GetPixel(w, h).B
                    );
                bmp.SetPixel(w, h, c);
            }
        }
    ii.Set(bmp, null, Color.Transparent);
}
  • have you tried another format? I suspect you are saving it as old ICO format, which does not support alpha, but key color (because that you are passing Color.Transparent to Set) – Gusman Apr 24 '14 at 19:54
  • I couldn't find any information on `IconImage`, but you should double check what `ii.Set(bmp, null, Color.Transparent);` is doing. – TyCobb Apr 24 '14 at 19:56
  • Also, instead of creating your bitmap with `new Bitmap(ii.Image);`you can try to create it this way: `new Bitmap(ii.Image.Width, ii.Image.Height, PixelFormat.Format32bppArgb);` to ensure it has transparency – Gusman Apr 24 '14 at 19:56

1 Answers1

0

You probably aren't using a proper PixelFormat. The format needs to support four-byte pixels. Refer to the following related question on how to change the format: Converting Bitmap PixelFormats in C#.

EDIT: You could try to use Graphics to draw to your bitmap like suggested in the following post: Icon to Image - transparency issue. Also, there is an article on CodeProject that seems to be doing exactly what you want.

Community
  • 1
  • 1
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80