Procedura: disegnare un'area con un colore a tinta unita

Per disegnare un'area con un colore a tinta unita, è possibile utilizzare un pennello di sistema predefinito, ad esempio Red o Blue, oppure è possibile crearne un nuovo SolidColorBrush e descriverne Color l'utilizzo usando valori alfa, rosso, verde e blu. In XAML è anche possibile disegnare un'area con un colore a tinta unita usando la notazione esadecimale.

Gli esempi seguenti usano ognuna di queste tecniche per disegnare un Rectangle blu.

Esempio

Uso di un pennello predefinito

Nell'esempio seguente viene utilizzato il pennello Blue predefinito per disegnare un rettangolo blu.

<Rectangle Width="50" Height="50" Fill="Blue" />
// Create a rectangle and paint it with
// a predefined brush.
Rectangle myPredefinedBrushRectangle = new Rectangle();
myPredefinedBrushRectangle.Width = 50;
myPredefinedBrushRectangle.Height = 50;
myPredefinedBrushRectangle.Fill = Brushes.Blue;

Uso della notazione esadecimale

L'esempio successivo usa la notazione esadecimale a 8 cifre per disegnare un rettangolo blu.

<!-- Note that the first two characters "FF" of the 8-digit
     value is the alpha which controls the transparency of 
     the color. Therefore, to make a completely transparent
     color (invisible), use "00" for those digits (e.g. #000000FF). -->
<Rectangle Width="50" Height="50" Fill="#FF0000FF" />

Uso dei valori ARGB

L'esempio seguente crea un oggetto SolidColorBrush e ne Color descrive l'utilizzo dei valori ARGB per il colore blu.

<Rectangle Width="50" Height="50">
  <Rectangle.Fill>
    <SolidColorBrush>
     <SolidColorBrush.Color>

        <!-- Describes the brush's color using
             RGB values. Each value has a range of 0-255.  
             R is for red, G is for green, and B is for blue.
             A is for alpha which controls transparency of the
             color. Therefore, to make a completely transparent
             color (invisible), use a value of 0 for Alpha. -->
        <Color A="255" R="0" G="0" B="255" />
     </SolidColorBrush.Color>
    </SolidColorBrush>
  </Rectangle.Fill>
</Rectangle>
Rectangle myRgbRectangle = new Rectangle();
myRgbRectangle.Width = 50;
myRgbRectangle.Height = 50;
SolidColorBrush mySolidColorBrush = new SolidColorBrush();

// Describes the brush's color using RGB values.
// Each value has a range of 0-255.
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);
myRgbRectangle.Fill = mySolidColorBrush;

Per altri modi di descrivere il colore, vedere la Color struttura.

Argomenti correlati

Per altre informazioni su SolidColorBrush e altri esempi, vedere panoramica di Painting with Solid Colors and Gradients Overview (Disegnare con colori a tinta unita e sfumature).

Questo esempio di codice fa parte di un esempio più ampio fornito per la SolidColorBrush classe . Per l'esempio completo, vedere Brushes Sample (Esempio di pennelli).

Vedi anche