Bitmap.SetPixel(Int32, Int32, Color) Bitmap.SetPixel(Int32, Int32, Color) Bitmap.SetPixel(Int32, Int32, Color) Method

Definizione

Imposta il colore del pixel specificato in questo oggetto Bitmap.Sets the color of the specified pixel in this Bitmap.

public:
 void SetPixel(int x, int y, System::Drawing::Color color);
public void SetPixel (int x, int y, System.Drawing.Color color);
member this.SetPixel : int * int * System.Drawing.Color -> unit

Parametri

x
Int32 Int32 Int32

Coordinata x del pixel da impostare.The x-coordinate of the pixel to set.

y
Int32 Int32 Int32

Coordinata y del pixel da impostare.The y-coordinate of the pixel to set.

color
Color Color Color

Struttura Color che rappresenta il colore da assegnare al pixel specificato.A Color structure that represents the color to assign to the specified pixel.

Eccezioni

Operazione non riuscita.The operation failed.

Esempi

L'esempio di codice seguente è progettato per l'uso con Windows Forms e richiede PaintEventArgs @ no__t-1, che è un parametro del gestore eventi Paint.The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. Nel codice vengono eseguite le seguenti azioni:The code performs the following actions:

  • Crea un oggetto Bitmap.Creates a Bitmap.

  • Imposta il colore di ogni pixel della bitmap su nero.Sets the color of each pixel in the bitmap to black.

  • Disegna la bitmap.Draws the bitmap.

private:
   void SetPixel_Example( PaintEventArgs^ e )
   {
      // Create a Bitmap object from a file.
      Bitmap^ myBitmap = gcnew Bitmap( "Grapes.jpg" );

      // Draw myBitmap to the screen.
      e->Graphics->DrawImage( myBitmap, 0, 0, myBitmap->Width, myBitmap->Height );

      // Set each pixel in myBitmap to black.
      for ( int Xcount = 0; Xcount < myBitmap->Width; Xcount++ )
      {
         for ( int Ycount = 0; Ycount < myBitmap->Height; Ycount++ )
         {
            myBitmap->SetPixel( Xcount, Ycount, Color::Black );
         }
      }

      // Draw myBitmap to the screen again.
      e->Graphics->DrawImage( myBitmap, myBitmap->Width, 0, myBitmap->Width, myBitmap->Height );
   }
private void SetPixel_Example(PaintEventArgs e)
{

    // Create a Bitmap object from a file.
    Bitmap myBitmap = new Bitmap("Grapes.jpg");

    // Draw myBitmap to the screen.
    e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width,
        myBitmap.Height);

    // Set each pixel in myBitmap to black.
    for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
    {
        for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
        {
            myBitmap.SetPixel(Xcount, Ycount, Color.Black);
        }
    }

    // Draw myBitmap to the screen again.
    e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0,
        myBitmap.Width, myBitmap.Height);
}
Private Sub SetPixel_Example(ByVal e As PaintEventArgs)

    ' Create a Bitmap object from a file.
    Dim myBitmap As New Bitmap("Grapes.jpg")

    ' Draw myBitmap to the screen.
    e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width, _
    myBitmap.Height)

    ' Set each pixel in myBitmap to black.
    Dim Xcount As Integer
    For Xcount = 0 To myBitmap.Width - 1
        Dim Ycount As Integer
        For Ycount = 0 To myBitmap.Height - 1
            myBitmap.SetPixel(Xcount, Ycount, Color.Black)
        Next Ycount
    Next Xcount

    ' Draw myBitmap to the screen again.
    e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0, myBitmap.Width, _
        myBitmap.Height)
End Sub

Commenti

Usare il metodo SetPixel per impostare il colore di un singolo pixel in un'immagine a livello di codice.Use SetPixel method to set the color of an individual pixel in an image programmatically. È anche possibile modificare un'immagine a livello di codice usando il metodo LockBits.You can also change an image programmatically by using the LockBits method. In genere, per le modifiche su larga scala, il metodo LockBits offre prestazioni migliori.Typically for large-scale changes, the LockBits method offers better performance.

Si applica a