嗨,我创建了一个图片框,当选择了一个效果时,它会使用颜色矩阵更改图片框中的图像。
我遇到的问题是,如果我选择一个效果时选择另一个效果,旧效果将不会消失,而是只会停留在选定的新效果下面。我现在使用的效果是棕褐色和灰度,但是任何人都可以帮助我,这样一旦选择了一个效果,旧效果就会被清除,而不是仅仅相互叠加。
我正在使用图形和colormatrix以及位图,这是我的两个按钮的代码:
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;
}
答案 0 :(得分: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
以使任何下一个效果适用或。