0

I'm making a program with color quantization, after the quantization is finished, I want to display all the colors from the used color palette.

I can display them correctly once, but if u restart the process(press the button again) and say that there needs to be less colors or diffrent collors , the displayed colors are staying the colors from the first time I pressed the button.

This is what creates the boxes with colors and displays them on the screen :

private void CreatePallette(List<RGB> colorPallette)
    {

        var picture = new PictureBox();
        int heightPixel = 0;
        int widthPixel = 0;
        for (int k = 0; k < colorPallette.Count(); k++)
        {
            Bitmap box = new Bitmap(25, 25);
            RGB color = colorPallette[k];
            Color adding = Color.FromArgb(color.GetR(), color.GetG(), color.GetB());
            for (int i = 0; i < box.Height; i++)
            {
                for (int y = 0; y < box.Width; y++)
                {
                    box.SetPixel(i, y, adding);
                }
            }
            picture = new PictureBox
            {
                Name = "pictureBoxColor" + k,
                Size = new Size(32, 32),
                Location = new Point(50 + (25 * widthPixel), (25 * heightPixel) + 400),
                Image = box
            };
            picture.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.Controls.Add(picture);
            widthPixel++;
            if(widthPixel == 33)
            {
                heightPixel++;
                widthPixel = 0;
            }
        }
    }

What I want that happens is if the color palette has 256 colors , 256boxes need to be on the form, if there are only 64 colors only 64 boxes need to be on the screen.

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • Why create so many pboxes??? Why do you overwrite picture in all the time the loop? Why do you use SetPixel instead of setting the BackColor?? But the best course of action imo is to have a single pbox and a list. In the pbox.Paint you can use e.Graphics.FillRectangle to display the colors in a loop. Always try to avoid adding large numers of controls! – TaW Nov 05 '19 at 16:12
  • You should follow TaW's suggestion, though. A `List` can be used to paint colored squares into a single Bitmap, then show the Bitmap in a single PictureBox. A `List` would be better. The class object can have a `Rectangle` and `Color` properties. When the PictureBox is clicked, you can determine what rectangle contains the Point clicked with the mouse, using the `Rectangle.Contains(Point)` method. So you can query the `List` to determine the color used in the rectagular area of interest. – Jimi Nov 05 '19 at 19:03

0 Answers0