Windows.Forms - Change ImageList Bitmaps pixels for HighContrast

285 views Asked by At

Need help with altering the Bitmaps in an ImageList to switch the black pixels to yellow, they are being used as images for a UICommandBar (screenshot and attempt below)

UICommandBar images not visible when in HighContrast

The code gets executed, with GetPixel condition and then SetPixel, but the images are not changed. Maybe related to ImageListStreamer or not the right time to swap those pixels.

rtfeditor.cs

this.imageList1.ImageStream = ( (System.Windows.Forms.ImageListStreamer)(resources.GetObject( "imageList1.ImageStream" ) ) );

this.imageList1 = ColorSpace.ProcessHighContrast(this.imageList1);

Utils\ColorSpace.cs

public static System.Windows.Forms.ImageList ProcessHighContrast(System.Windows.Forms.ImageList imageList)
                 {
                         if (System.Windows.Forms.SystemInformation.HighContrast)
                         {
                                 foreach (System.Drawing.Bitmap imageListImage in imageList.Images)
                                  {
                                          for (int i = 0; i < imageListImage.Width; i++)
                                          {
                                                  for (int j = 0; j < imageListImage.Height; j++)
                                                   {
                                                           Color color =     imageListImage.GetPixel(i, j);
                                                           if (System.Drawing.Color.FromArgb(255, 0, 0, 0).Equals(color))
                                                                  imageListImage.SetPixel(i, j, System.Drawing.SystemColors.WindowText);
                                                   }
                                          }
                                  }
                         }

                         return imageList;
                 }
1

There are 1 answers

0
siliond On BEST ANSWER

Solved enter image description here

Hans Passant's comment was critical for resolving this (see quoted comment below) + changing the order of the call of ProcessHighContrast (now first) with the assignment of the ImageList to the Janus.Windows.UI.CommandBars.UICommandBar (now second). New method and calling code below.

ImageList.Images returns a copy of each image. Altering the copy therefore does not modify the ImageList content at all. Create a new ImageList instead. Do consider that Darth Vader color schemes tend to be appreciated only by programmers, regular users expect the high contrast version to have a white background. That's going to be a lot less painful. – Hans Passant

rtfeditor.cs

        this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));

        this.imageList1 = ColorSpace.ProcessHighContrast(this.imageList1);

        this.uiCommandManager1.ImageList = this.imageList1;

Utils\ColorSpace.cs

        public static System.Windows.Forms.ImageList ProcessHighContrast(System.Windows.Forms.ImageList imageList)
    {
        if (System.Windows.Forms.SystemInformation.HighContrast)
        {
            System.Windows.Forms.ImageList imageListNew = new ImageList();

            foreach (System.Drawing.Bitmap imageListImage in imageList.Images)
            {
                for (int i = 0; i < imageListImage.Width; i++)
                {
                    for (int j = 0; j < imageListImage.Height; j++)
                    {
                        Color color = imageListImage.GetPixel(i, j);

                        if (System.Drawing.Color.FromArgb(255, 0, 0, 0).Equals(color))
                            imageListImage.SetPixel(i, j, System.Drawing.SystemColors.WindowText);
                    }
                }

                imageListNew.Images.Add(imageListImage);
            }

            return imageListNew;
        }

        return imageList;
    }