Color.ToArgb Metoda

Definicja

Pobiera 32-bitową wartość ARGB tej struktury Color.

C#
public int ToArgb();

Zwraca

32-bitowa wartość ARGB tego Color.

Przykłady

Poniższy przykład kodu jest przeznaczony do użycia z formularzami Systemu Windows i wymaga PaintEventArgse, który jest parametrem programu obsługi zdarzeń Paint. Kod wykonuje następujące akcje:

  • Iteruje za pomocą elementów wyliczania KnownColor, aby znaleźć wszystkie znane kolory, które mają niezerowy składnik zielony i składnik czerwony o zerowej wartości, a które nie są kolorami systemowymi.

  • Podczas każdej iteracji zapisuje element KnownColor — jeśli spełnia kryteria — w tablicy.

  • Używa pędzla do malowania prostokątów.

Każdy prostokąt jest malowany KnownColor zgodny z kryteriami określonymi w pierwszym punkcie. Wyświetlana jest również nazwa KnownColor i jego wartości składników.

W tym przykładzie są wyświetlane niektóre znane kolory, nazwy kolorów i ich cztery wartości składników. Metoda ToArgb jest używana jako wstępny krok do wyświetlania wartości składników.

C#
      public void ToArgbToStringExample1(PaintEventArgs e)
      {
          Graphics     g = e.Graphics;
                   
          // Color structure used for temporary storage.
          Color   someColor = Color.FromArgb(0);
                   
          // Array to store KnownColor values that match the criteria.
          KnownColor[]  colorMatches = new KnownColor[167];
          
          // Number of matches found.
          int  count = 0; 

          // Iterate through the KnownColor enums to find all corresponding colors
          // that have a nonzero green component and zero-value red component and
          // that are not system colors.
          for (KnownColor enumValue = 0;
              enumValue <= KnownColor.YellowGreen; enumValue++)
          {
              someColor = Color.FromKnownColor(enumValue);
              if (someColor.G != 0 && someColor.R == 0 && !someColor.IsSystemColor)
                  colorMatches[count++] = enumValue;
          }
          SolidBrush  myBrush1 = new SolidBrush(someColor);
          Font        myFont = new Font("Arial", 9);
          int         x = 40;
          int         y = 40;
                   
          // Iterate through the matches that were found and display each color that
          // corresponds with the enum value in the array. also display the name of
          // the KnownColor and the ARGB components.
          for (int i = 0; i < count; i++)
          {
                   
              // Display the color.
              someColor = Color.FromKnownColor(colorMatches[i]);
              myBrush1.Color = someColor;
              g.FillRectangle(myBrush1, x, y, 50, 30);
                   
              // Display KnownColor name and the four component values. To display the
              // component values:  Use the ToArgb method to get the 32-bit ARGB value
              // of someColor, which was created from a KnownColor. Then create a
              // Color structure from the 32-bit ARGB value and set someColor equal to
              // this new Color structure. Then use the ToString method to convert it to
              // a string.
              g.DrawString(someColor.ToString(), myFont, Brushes.Black, x + 55, y);
              someColor = Color.FromArgb(someColor.ToArgb());
              g.DrawString(someColor.ToString(), myFont, Brushes.Black, x + 55, y + 15);
              y += 40;
          }
      }

Uwagi

Kolejność bajtów 32-bitowej wartości ARGB to AARRGGBB. Najbardziej znaczący bajt (MSB), reprezentowany przez usługę AA, jest wartością składnika alfa. Drugie, trzecie i czwarte bajty, reprezentowane odpowiednio przez RR, GG i BB, są składnikami kolorów czerwonymi, zielonymi i niebieskimi, odpowiednio

Dotyczy

Produkt Wersje
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1