0

Hi, I have created a picturebox which, when a an effect is selected, it will then change the image in the picture box using the color matrix.

我遇到的问题是,如果我在选择另一个效果时选择了另一个效果,那么旧效果不会消失,而是会停留在那里并位于所选新效果的下方。我现在使用的效果是棕褐色和灰度,但是任何人都可以帮助我,这样一旦选择了一种效果,旧的效果就会被清除,而不是它们只是相互叠加。

我正在使用图形和颜色矩阵以及位图,这是我的两个按钮的代码:

Graphics g;

private void greyscalePicture_Click(object sender, EventArgs e)
{

   Image img = pictureBox.Image;
   Bitmap greyscaleBitmap = new Bitmap(img.Width, img.Height);
   ImageAttributes ia = new ImageAttributes();
   ColorMatrix cmImage = new ColorMatrix(new float[][]
   {
      new float[] {.3f, .3f, .3f, 0, 0}, 
      new float[] {.59f, .59f, .59f, 0, 0}, 
      new float[] {.11f, .11f, .11f, 0, 0}, 
      new float[] {0, 0, 0, 1, 0}, 
      new float[] {0, 0, 0, 0, 1}
   });
   ia.SetColorMatrix(cmImage);
   g = Graphics.FromImage(greyscaleBitmap);
   g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0,
               img.Width, img.Height, GraphicsUnit.Pixel, ia);
   g.Dispose();
   pictureBox.Image = greyscaleBitmap;
}

// This is the same as the grey effect except
// the float values have been changed
private void sepiaPicture_Click(object sender, EventArgs e)
{
   Image img = pictureBox.Image;
   Bitmap sepiaBitmap = new Bitmap(img.Width, img.Height);
   ImageAttributes ia = new ImageAttributes();
   ColorMatrix cmImage = new ColorMatrix(new float[][]
   {
      new float[] {.393f, .349f, .272f, 0, 0}, 
      new float[] {.769f, .686f, .534f, 0, 0}, 
      new float[] {.189f, .168f, .131f, 0, 0}, 
      new float[] {0, 0, 0, 1, 0}, 
      new float[] {0, 0, 0, 0, 1} 
   });
   ia.SetColorMatrix(cmImage);
   g = Graphics.FromImage(sepiaBitmap);
   g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0,
               img.Width, img.Height, GraphicsUnit.Pixel, ia);
   g.Dispose();
   pictureBox.Image = sepiaBitmap;
}
4

1 回答 1

1

您必须存储原始图像,将效果应用于此原始图像不是当前图像:

//Your form constructor
public Form1(){
   InitializeComponent();
   originalImage = pictureBox.Image;
}
Image originalImage;
private void greyscalePicture_Click(object sender, EventArgs e) {
 Image img = originalImage;// Not pictureBox.Image
 //...
}
private void sepiaPicture_Click(object sender, EventArgs e) {
 Image img = originalImage;// Not pictureBox.Image
 //...
}

关键是每当您想保存当前状态时,只需更新您的originalImage以使任何下一个效果交替应用。

于 2013-11-06T15:19:05.020 回答