本文介绍了将图片框中的图像更改为棕褐色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a picturebox and want to change the image color to sepia i know what to do so far in terms of setting it to grayscale then filtering it but the final part is my downfall can someone help set this to sepia for me by suggesting what i should do from the comments that i have provided thanks a lot

推荐答案

你的代码可以归结为:

    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 来表示如何转换颜色,然后将图像重新绘制为一个新的 Bitmap,并使用 ColorMatrix 通过 ImageAttributes 传递它:

This, however, is a pretty slow set of nested loops. A faster approach is to create a ColorMatrix that represents how to transform the colors and then redraw the image into a new Bitmap passing it thru an ImageAttributes using the ColorMatrix:

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

我从这篇文章中获得了棕褐色色调值.

I got the sepia tone values from this article.

这篇关于将图片框中的图像更改为棕褐色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:19