在我操作它时显示图像

时间:2013-11-01 13:35:34

标签: c# image-processing

我正在编写一个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;
            }
        }

    }
}

1 个答案:

答案 0 :(得分: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();
}

更改后,结果(使用我的测试图像)如下所示:

test form

相关问题