none
Windows.Forms - Change ImageList Bitmaps pixels for HighContrast RRS feed

  • Question

  • 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)




    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

    // 

    // imageList1

    // 

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

    this.imageList1.TransparentColor = System.Drawing.Color.White;

    this.imageList1.Images.SetKeyName( 0, "" );

    this.imageList1.Images.SetKeyName( 1, "" );

    this.imageList1.Images.SetKeyName( 2, "" );

    this.imageList1.Images.SetKeyName( 3, "" );

    this.imageList1.Images.SetKeyName( 4, "" );

    this.imageList1.Images.SetKeyName( 5, "" );

    this.imageList1.Images.SetKeyName( 6, "" );

    this.imageList1.Images.SetKeyName( 7, "" );

    this.imageList1.Images.SetKeyName( 8, "" );

    this.imageList1.Images.SetKeyName( 9, "" );

    this.imageList1.Images.SetKeyName( 10, "" );

    this.imageList1.Images.SetKeyName( 11, "" );

    this.imageList1.Images.SetKeyName( 12, "" );

    this.imageList1.Images.SetKeyName( 13, "" );

    this.imageList1.Images.SetKeyName( 14, "" );

    this.imageList1.Images.SetKeyName( 15, "" );

    this.imageList1.Images.SetKeyName( 16, "" );

    this.imageList1.Images.SetKeyName( 17, "" );

    this.imageList1.Images.SetKeyName( 18, "" );

    this.imageList1.Images.SetKeyName( 19, "" );

    this.imageList1.Images.SetKeyName( 20, "" );



    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;
                     }



    Daniel Silion

    Tuesday, September 5, 2017 11:48 AM

Answers

  • Hi Daniel Silion,

    Based on my search, I do not find UICommandBar. Maybe it defined by yourself. I test the code without UICommandBar. According to your code below, the (255,0,0,0) means Black, and the SystemColors.WindowText is also Black. It switches the Black to Black.

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

    The pixel (255,0,0,0) is for Black, but the Black we see is not only the pixel as (255,0,0,0).

    You could refer to the following code. It switches Black (255,0,0,0) to yellow (255,255,255,0) for your reference.

      void Setpiexl()
            {
                Bitmap bit = new Bitmap(@"2.jpg");
                int width = bit.Width;
                int height = bit.Height;
                for (int i = 1; i < width; i++)
                {
                    for (int j = 1; j < height; j++)
                    {
                        Color c = bit.GetPixel(i, j);
                        if (c == Color.FromArgb(255, 0, 0, 0))
                        {
                            bit.SetPixel(i, j, Color.FromArgb(255, 255, 255, 0));
                        }
                    }
                }
                this.pictureBox1.Width = bit.Width;
                this.pictureBox1.Height = bit.Height;
                pictureBox2.Image = bit;
            }

    The left one is the original picture. The right one is the picture which switches Black to Yellow.

    You could see that not all the Black Pixel (255,0,0,0) equal to the Black we see.

    In your screenshot, the part which not convert Black (255,0,0,0) to yellow, maybe it not the pixel(255,0,0,0). If you do not want to convert white to Black, you could refer to my code.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 7, 2017 6:07 AM
    Moderator

All replies

  • Hi Daniel Silion,

    Based on my search, I do not find UICommandBar. Maybe it defined by yourself. I test the code without UICommandBar. According to your code below, the (255,0,0,0) means Black, and the SystemColors.WindowText is also Black. It switches the Black to Black.

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

    The pixel (255,0,0,0) is for Black, but the Black we see is not only the pixel as (255,0,0,0).

    You could refer to the following code. It switches Black (255,0,0,0) to yellow (255,255,255,0) for your reference.

      void Setpiexl()
            {
                Bitmap bit = new Bitmap(@"2.jpg");
                int width = bit.Width;
                int height = bit.Height;
                for (int i = 1; i < width; i++)
                {
                    for (int j = 1; j < height; j++)
                    {
                        Color c = bit.GetPixel(i, j);
                        if (c == Color.FromArgb(255, 0, 0, 0))
                        {
                            bit.SetPixel(i, j, Color.FromArgb(255, 255, 255, 0));
                        }
                    }
                }
                this.pictureBox1.Width = bit.Width;
                this.pictureBox1.Height = bit.Height;
                pictureBox2.Image = bit;
            }

    The left one is the original picture. The right one is the picture which switches Black to Yellow.

    You could see that not all the Black Pixel (255,0,0,0) equal to the Black we see.

    In your screenshot, the part which not convert Black (255,0,0,0) to yellow, maybe it not the pixel(255,0,0,0). If you do not want to convert white to Black, you could refer to my code.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 7, 2017 6:07 AM
    Moderator
  • I was out of the office due to hurricane Irma, back to work today. I'm in the process of giving it a try - thank you very much

    Daniel Silion

    Wednesday, September 13, 2017 12:14 PM
  • Hi Wendy,

    I needed this for adding support for HighContrast mode for a RichTextBox control.

    System.Drawing.SystemColors.WindowText is yellow when in high contrast.

    Thank you very much for your expertise. I managed to address this with similar suggestion, at larger scale (ImageList vs individual Bitmap), from StackOverflow, Windows.Forms - Change ImageList Bitmaps pixels for HighContrast.


    Daniel Silion

    Wednesday, September 13, 2017 1:02 PM