英語で読む

次の方法で共有


Image.PixelFormat プロパティ

定義

この Image のピクセル形式を取得します。

C#
public System.Drawing.Imaging.PixelFormat PixelFormat { get; }

プロパティ値

この PixelFormat のピクセル形式を表す Image

次のコード例では、 メソッドと SetPixel メソッドを使用してイメージの色を変更してGetPixel、ファイルから新しいビットマップを構築する方法を示します。 また、 プロパティも使用します PixelFormat

この例は、、および Button という名前Button1Label1PictureBox1をそれぞれ含む LabelPictureBox Windows フォームで使用するように設計されています。 コードをフォームに貼り付け、 メソッドを Button1_Click ボタンの Click イベントに関連付けます。

C#
Bitmap image1;

private void Button1_Click(System.Object sender, System.EventArgs e)
{

    try
    {
        // Retrieve the image.
        image1 = new Bitmap(@"C:\Documents and Settings\All Users\" 
            + @"Documents\My Music\music.bmp", true);

        int x, y;

        // Loop through the images pixels to reset color.
        for(x=0; x<image1.Width; x++)
        {
            for(y=0; y<image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
                image1.SetPixel(x, y, newColor);
            }
        }

        // Set the PictureBox to display the image.
        PictureBox1.Image = image1;

        // Display the pixel format in Label1.
        Label1.Text = "Pixel format: "+image1.PixelFormat.ToString();
    }
    catch(ArgumentException)
    {
        MessageBox.Show("There was an error." +
            "Check the path to the image file.");
    }
}

適用対象

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