c# - 将图片框中的图像更改为棕褐色

标签 c# visual-studio-2010 picturebox

我有一个图片框,想将图像颜色更改为棕褐色 我知道到目前为止该怎么做,将其设置为灰度然后对其进行过滤,但最后一部分是我的垮台有人可以帮助我将其设置为棕褐色吗根据我提供的评论建议我应该做什么,非常感谢

最佳答案

您的代码可以归结为:

    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;
    }

我从 this 得到棕褐色调值文章。

关于c# - 将图片框中的图像更改为棕褐色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19542174/

相关文章:

c++ - 在 Visual Studio 的解决方案资源管理器中组织项目文件

c# - 如何加载图像,然后等待几秒钟,然后播放 mp3 声音?

c# - c# 中的 c++ 锯齿状数组切片

c# - 如何在 .net Core 中记录授权尝试

visual-studio-2010 - 如何在 Visual Studio 2010 的输出窗口中设置制表符大小?

visual-studio-2010 - 如何找到Hadoop将在其下执行作业的帐户的名称

c# - 将 url 中的图像加载到 PictureBox 中

c# for-loop 和 Click 事件处理程序

c# - Predicates 或 Actions 是否具有任何其他属性或特性以将它们与 Funcs 区分开来?

c# - Socket组件TcpClient可以使用http代理吗?