0

画像内のピクセルをスキャンする C# プログラムを作成しています。これは、おそらく 5 回しか実行しないプログラムのためのものなので、効率的である必要はありません。

各ピクセルを読み取った後、色を変更し、画像の表示を更新して、スキャンの進行状況と検出内容を確認できるようにしたいのですが、何も表示できません。誰かが前にこのようなことをしたことがありますか、または良い例を知っていますか?

編集:私が現在使用しようとしているコードは.

void Main()
{
    Bitmap b = new Bitmap(@"C:\test\RKB2.bmp");
    Form f1 = new Form();
    f1.Height = (b.Height /10);
    f1.Width = (b.Width / 10);
    PictureBox PB = new PictureBox();
    PB.Image = (Image)(new Bitmap(b, new Size(b.Width, b.Height)));
    PB.Size = new Size(b.Width /10, b.Height /10);
    PB.SizeMode = PictureBoxSizeMode.StretchImage;
    f1.Controls.Add(PB);
    f1.Show();
    for(int y = 0; y < b.Height; y++)
    {
        for(int x = 0; x < b.Width; x++)
        {
            if(b.GetPixel(x,y).R == 0 && b.GetPixel(x,y).G == 0 && b.GetPixel(x,y).B == 0)
            {
                //color is black
            }
            if(b.GetPixel(x,y).R == 255 && b.GetPixel(x,y).G == 255 && b.GetPixel(x,y).B == 255)
            {
                //color is white
                Bitmap tes = (Bitmap)PB.Image;
                tes.SetPixel(x, y, Color.Yellow);
                PB.Image = tes;
            }
        }

    }
}
4

1 に答える 1

0

画像処理アクションと更新アクションを分離する必要があります。まず、Windows フォーム プロジェクトを作成し、PictureBox コントロールをフォームとボタンに追加します。ボタンをアクションにバインドし、アクションで処理を開始します。その後、更新プロセスが表示されます。コードを次のように変更すると、最終的な変換が表示されます。

void Main()
{
    Bitmap b = new Bitmap(@"C:\test\RKB2.bmp");
    Form f1 = new Form();
    f1.Height = (b.Height /10);
    f1.Width = (b.Width / 10);
    // size the form
    f1.Size = new Size(250, 250);
    PictureBox PB = new PictureBox();
    PB.Image = (Image)(new Bitmap(b, new Size(b.Width, b.Height)));
    PB.Size = new Size(b.Width /10, b.Height /10);
    PB.SizeMode = PictureBoxSizeMode.StretchImage;
    PB.SetBounds(0, 0, 100, 100);
    Button start = new Button();
    start.Text = "Start processing";
    start.SetBounds(100, 100, 100, 35);
    // bind the button Click event
    // The code could be also extracted to a method:
    // private void startButtonClick(object sender, EventArgs e) 
    // and binded like this: start.Click += startButtonClick;
    start.Click += (s, e) =>
    {
       for(int y = 0; y < b.Height; y++)
        {
            for(int x = 0; x < b.Width; x++)
            {
                if(b.GetPixel(x,y).R == 0 && b.GetPixel(x,y).G == 0 && b.GetPixel(x,y).B == 0)
                {
                    //color is black
                }
                if(b.GetPixel(x,y).R == 255 && b.GetPixel(x,y).G == 255 && b.GetPixel(x,y).B == 255)
                {
                    //color is white
                    Bitmap tes = (Bitmap)PB.Image;
                    tes.SetPixel(x, y, Color.Yellow);
                    PB.Image = tes;
                }
            }
        }
    };
    f1.Controls.Add(PB);
    f1.Controls.Add(start);
    f1.Show();
}

変更後の結果 (私のテスト画像) は次のようになります。

テストフォーム

于 2013-11-01T15:06:47.130 に答える