Bitmap.SetPixel(Int32, Int32, Color) Método

Definición

Establece el color en el píxel especificado en este 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
Public Sub SetPixel (x As Integer, y As Integer, color As Color)

Parámetros

x
Int32

Coordenada X del píxel que se va a establecer.

y
Int32

Coordenada y del píxel que se va a establecer.

color
Color

Color estructura que representa el color que se va a asignar al píxel especificado.

Excepciones

Error en la operación.

Ejemplos

El ejemplo de código siguiente está diseñado para su uso con Windows Forms y requiere PaintEventArgse, que es un parámetro del Paint controlador de eventos. El código realiza las siguientes acciones:

  • Crea una interfaz Bitmap.

  • Establece el color de cada píxel del mapa de bits en negro.

  • Dibuja el mapa de bits.

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

Comentarios

Use SetPixel el método para establecer el color de un píxel individual en una imagen mediante programación. También puede cambiar una imagen mediante programación mediante el LockBits método . Normalmente, para los cambios a gran escala, el LockBits método ofrece un mejor rendimiento.

Se aplica a