英語で読む

次の方法で共有


Bitmap.GetPixel(Int32, Int32) メソッド

定義

この Bitmap内の指定したピクセルの色を取得します。

public System.Drawing.Color GetPixel (int x, int y);

パラメーター

x
Int32

取得するピクセルの x 座標。

y
Int32

取得するピクセルの y 座標。

戻り値

指定したピクセルの色を表す Color 構造体。

例外

x が 0 未満か、Width以上です。

-又は-

y が 0 未満か、Height以上です。

操作が失敗しました。

次のコード例は Windows フォームで使用できるように設計されており、Paint イベント ハンドラーのパラメーターである PaintEventArgseが必要です。 このコードは、ビットマップ内のピクセルの色を取得し、その色で四角形を塗りつぶします。

private void GetPixel_Example(PaintEventArgs e)
{

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

    // Get the color of a pixel within myBitmap.
    Color pixelColor = myBitmap.GetPixel(50, 50);

    // Fill a rectangle with pixelColor.
    SolidBrush pixelBrush = new SolidBrush(pixelColor);
    e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100);
}

適用対象

製品 バージョン
.NET 6, 7, 8, 9
.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
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9