我有一个图片框,想将图像颜色更改为棕褐色,到目前为止,我知道将其设置为灰度然后对其进行过滤,但最后一部分是我的失败,有人可以通过建议我将其设置为棕褐色来帮助我吗应该从我提供的评论中做,非常感谢
user2724441
问问题
2506 次
1 回答
4
您的代码可以归结为:
private void button1_Click(object sender, EventArgs e)
{
Bitmap sepiaEffect = (Bitmap)pictureBox.Image.Clone();
for (int yCoordinate = 0; yCoordinate < sepiaEffect.Height; yCoordinate++)
{
for (int xCoordinate = 0; xCoordinate < sepiaEffect.Width; xCoordinate++)
{
Color color = sepiaEffect.GetPixel(xCoordinate, yCoordinate);
double grayColor = ((double)(color.R + color.G + color.B)) / 3.0d;
Color sepia = Color.FromArgb((byte)grayColor, (byte)(grayColor * 0.95), (byte)(grayColor * 0.82));
sepiaEffect.SetPixel(xCoordinate, yCoordinate, sepia);
}
}
pictureBox.Image = sepiaEffect;
}
然而,这是一组非常慢的嵌套循环。一种更快的方法是创建一个ColorMatrix,它表示如何转换颜色,然后将图像重绘为一个新的位图,并使用 ColorMatrix 通过 ImageAttributes 将其传递:
private void button2_Click(object sender, EventArgs e)
{
float[][] sepiaValues = {
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}};
System.Drawing.Imaging.ColorMatrix sepiaMatrix = new System.Drawing.Imaging.ColorMatrix(sepiaValues);
System.Drawing.Imaging.ImageAttributes IA = new System.Drawing.Imaging.ImageAttributes();
IA.SetColorMatrix(sepiaMatrix);
Bitmap sepiaEffect = (Bitmap)pictureBox.Image.Clone();
using (Graphics G = Graphics.FromImage(sepiaEffect))
{
G.DrawImage(pictureBox.Image, new Rectangle(0, 0, sepiaEffect.Width, sepiaEffect.Height), 0, 0, sepiaEffect.Width, sepiaEffect.Height, GraphicsUnit.Pixel, IA);
}
pictureBox.Image = sepiaEffect;
}
我从这篇文章中得到了棕褐色调值。
于 2013-10-23T15:32:01.417 回答