-1

What I am trying to do is after a event happens i set all my arrow pictures to purple.

Here is what my arrow picture looks like before drawing: Arrow to the left

Here is what it looks like after drawing: Arrow left after collored

First I make a list of all the pictureboxes with the arrow:

 List<PictureBox> arrows = new List<PictureBox>();
 foreach (var item in Controls.OfType<PictureBox>())
        {
            if (item.Name.StartsWith("arrow"))
            {
                arrows.Add(item);
            }    
        }

And this is the code I use to color the pictures:

System.Drawing.Color purple = System.Drawing.Color.Purple;

        foreach (var item in arrows)
        {
            Bitmap bmp = new Bitmap(item.Image, item.Height, item.Width);
            for (int i = 0; i < item.Height; i++)
            {
                for (int j = 0; j < item.Width; j++)
                {
                    var actualColor = bmp.GetPixel(i, j).ToArgb();
                    var purpleA = bmp.GetPixel(i, j).A;
                    if (actualColor != System.Drawing.Color.White.ToArgb())
                    {
                        bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(purpleA, purple));
                    } else
                    {
                        bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(actualColor));
                    }
                }
            }
            item.Image = bmp;
        }

How can I color the image accurately? At the moment the purple is really badly drawn. I need it to be the exact same as the black arrow but instead of black I need it to be purple.

Note: I resize the black arrow image when I put it in the picturebox, so on the form the black and the purple arrow are the same size. I uploaded the purple arrow with a screenshot and the black arrow was from my computer. That's why they are not the same size.

Mathijs
  • 177
  • 3
  • 18
  • What do you mean by precisely? Where is the actual issue? – ViRuSTriNiTy Oct 27 '16 at 12:37
  • I posted the pictures. Look how bad the purple is drawn. I need the arrow to be the exact same, but instead of black I need it to be purple. – Mathijs Oct 27 '16 at 12:38
  • There must be an issue with the alpha channel. Try to compare the red green and blue parts with 255, then it's a white color. – ViRuSTriNiTy Oct 27 '16 at 12:43
  • Possible duplicate of [How to Change Pixel Color of an Image in C#.NET](http://stackoverflow.com/questions/17208254/how-to-change-pixel-color-of-an-image-in-c-net) – ViRuSTriNiTy Oct 27 '16 at 12:46
  • Is there a difference between jpeg and png? I now have the images as .png. The background of the images should be white. But when I use `if (actualColor != 255)` I color the whole image purple. Even the white parts. – Mathijs Oct 27 '16 at 12:47
  • Please have a look at the possible duplicate link. It seems like you didn't understand how a bitmap is stored in memory. There are red, green and blue parts for each pixel, thus the comparision needs to be `if (actualColor.R != 255 && actualColor.G != 255 && actualColor.B != 255) ...`. And no, a `Bitmap` is neither a jpeg nor a png, its just pixel data in memory. You can save it to JPG or PNG, but thats another story. – ViRuSTriNiTy Oct 27 '16 at 12:50

2 Answers2

0

The control's dimensions are probably different from the image's.

Instead of item.Width use item.Image.Width etc.

EX:

var itemImage = item.Image;
Bitmap bmp = new Bitmap(itemImage, itemImage.Height, itemImage.Width);
Enfyve
  • 916
  • 10
  • 22
0

You could use the color map to replace a color by another one. it's much faster than looping for each pixels.

    Graphics g = pictureBox.GetGraphics; // get the picture box graphics (i doubt this line compile but you get the idea of the object you need)    
    using (Bitmap bmp = new Bitmap("img.bmp")) // or the image currently in the picture box
    {        
        // create the color map
        var colorMap = new ColorMap[1];
        colorMap[0] = new ColorMap();

        // old color
        colorMap[0].OldColor = Color.Black;

        // replaced by this color
        colorMap[0].NewColor = Color.Purple;

        // attribute to remap the table of colors
        var att = new ImageAttributes();
        att.SetRemapTable(colorMap);

        // draw result
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        g.DrawImage(bmp, rect, 0, 0, rect.Width, rect.Height, GraphicsUnit.Pixel, att);
    }
Franck
  • 4,438
  • 1
  • 28
  • 55