2013-11-01 73 views
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; 
      } 
     } 

    } 
} 
+1

你可以添加一些代码来显示你到目前为止做了什么? – littleimp

+2

这是一个WPF应用程序,或其他什么? –

+0

WinForms?使用'PictureBox'? –

回答

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

+0

他没有提到这是WinForms,WebForms,控制台还是别的。 –