Bitmap.SetPixel(Int32, Int32, Color) メソッド

定義

指定したピクセルの色を 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)

パラメーター

x
Int32

設定するピクセルの x 座標。

y
Int32

設定するピクセルの y 座標。

color
Color

指定したピクセルに割り当てる色を表す Color 構造体。

例外

操作が失敗しました。

次のコード例は、Windows フォームで使用するように設計されており、イベント ハンドラーのPaintパラメーターである が必要PaintEventArgseです。 コードは、次のアクションを実行します。

  • 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

注釈

メソッドを使用して SetPixel 、プログラムによって画像内の個々のピクセルの色を設定します。 メソッドを使用してプログラムでイメージを LockBits 変更することもできます。 通常、大規模な変更の場合、 メソッドはパフォーマンスを LockBits 向上させます。

適用対象