Question

I'm trying to change the pixel color of any pixel with a certain value. Looping through my pictures does work, selecting a pixel by a value also does fine, but when I try to change its value, nothing happens. I dont get an error, but after saving the MultiIcon (which is basicly a container for Icons) as new .ICO, it has the same Color as the original File.

private static void LoadIcons()
{

    foreach(string path in pathArray)
    {
        MultiIcon mi = new MultiIcon();
        mi.Load(path);
        listMi.Add(mi);
    }
}
private static void ChangeColor()
{
    foreach(MultiIcon mi in listMi)
        foreach (SingleIcon si in mi)
            for (int n = 0; n < si.Count(); n++)
                for (int w = 0; w < si[n].Image.Width; w++)
                    for (int h = 0; h < si[n].Image.Height; h++)
                        si[n].Image.SetPixel(w, h, Color.Red);
}
private static void SaveIcons()
{
    int i = 0;
    foreach(MultiIcon mi in listMi)
    {
        if(!Directory.Exists(directory+"/modified"))
            Directory.CreateDirectory(directory + "/modified");
        mi.Save(NewPath(pathArray[i]), MultiIconFormat.ICO);
        i++;
    }
}

MultiIcon comes from iconLib:

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

but I'm pretty sure, the Image.SetPixel() does not work, because, when I check a pixel's value directly after changing it, it's the value it had before.

Was it helpful?

Solution

Here is the working code. Note that I create new Bitmap based on .Image, because if original image is indexed-color image, then you cannot use .SetPixel method.

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 img = new Bitmap(ii.Image);
                for (int w = 0; w < img.Width; w++)
                    for (int h = 0; h < img.Height; h++)
                        img.SetPixel(w, h, Color.Red);
                ii.Set(img, null, Color.Transparent);
            }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top