2010-12-14 58 views
0

我使用AForge.net进行频道过滤,我有3个按钮,红色,蓝色和绿色。 当我点击按钮红色时,它将应用红色通道的过滤器。但是,当我继续单击蓝色按钮时,它将重叠红色并且图像变暗。删除图像上的过滤器

有谁知道当蓝色点击时我该如何“处置”红色通道,反之亦然,因此滤镜不会相互重叠?以下是我的代码片段。

private void redchannel_Click_1(object sender, EventArgs e) 
    { 
     try 
     { 

      pictureBox1.Image = pic; 
      pictureBox2.Image = pic2; 

      // create filter 
      ChannelFiltering filter = new ChannelFiltering(); 
      // set channels' ranges to keep 
      filter.Red = new IntRange(0, 255); 
      filter.Green = new IntRange(255, 255); 
      filter.Blue = new IntRange(255, 255); 
      // apply the filter 
      filter.ApplyInPlace(pic2); 


     } 


     catch (Exception) 
     { 
      throw new ApplicationException("Failed loading image"); 
     } 
    } 

    private void bluechannel_Click_1(object sender, EventArgs e) 
    { 
     try 
     { 
      pictureBox1.Image = pic; 
      pictureBox2.Image = pic2; 

      // create filter 
      ChannelFiltering filter = new ChannelFiltering(); 
      // set channels' ranges to keep 
      filter.Red = new IntRange(255, 255); 
      filter.Green = new IntRange(255, 255); 
      filter.Blue = new IntRange(0, 255); 
      // apply the filter 
      filter.ApplyInPlace(pic2); 

     } 




     catch (Exception) 
     { 
      throw new ApplicationException("Failed loading image"); 
     } 
    } 

    private void greenchannel_Click_1(object sender, EventArgs e) 
    { 
     try 
     { 
      pictureBox1.Image = pic; 
      pictureBox2.Image = pic2; 

      // create filter 
      ChannelFiltering filter = new ChannelFiltering(); 
      // set channels' ranges to keep 

      filter.Red = new IntRange(255, 255); 
      filter.Green = new IntRange(0, 255); 
      filter.Blue = new IntRange(255, 255); 
      // apply the filter 
      filter.ApplyInPlace(pic2); 
     } 
     catch (Exception) 
     { 
      throw new ApplicationException("Failed loading image"); 

     } 
    } 
+1

代码因式分解是一件好事! – 2010-12-14 07:47:41

+0

哈哈是赞同! – LouisL 2010-12-14 08:22:11

回答

2

您需要存储原始图像以及潜在修改的显示图像。对原稿进行计算并用显示图像显示。切勿改变原稿,只改变显示图像。

+0

是的,我在我的程序中这样做。我有2个pictureBox。 pictureBox1是我的原始图像,我从中获取像素,并在pictureBox2图像上设置像素。 – LouisL 2010-12-14 08:12:45

+0

然后,你不需要“删除”任何东西,我'[不知道是什么问题。 – 2010-12-14 18:12:48

1

我最好的猜测是你现在正在做的是你初始化pic2作为原始图片的副本。然后你继续添加过滤器到pic2。所以会发生什么是你有干净的图像,然后应用第一个过滤器,第二个过滤器等等。

我会改变:

pictureBox1.Image = pic; 
pictureBox2.Image = pic2; 

pictureBox1.Image = pic; 
pictureBox2.Image = pic.clone(); 

的另一件事是,你可能会想点所有3个按钮的功能相同。目前你所拥有的3个功能中有90%是代码重复。维护的噩梦。

更好的解决方案是: 另一种方法是将每个按钮分开(3 btn点击功能),在那里用红色,绿色&蓝色作为参数调用新的过滤功能。