1

所以我正在使用我在网上找到的东西的变体将图像转换为灰度。我希望能够“淡化为灰色”,即逐渐淡化颜色,这样我就可以从全色到部分着色到纯灰色。

public static ColorMatrix ColorMatrixForGrayScale = new ColorMatrix(new[] {
                             new[] {.3f, .3f, .3f, 0, 0},
                             new[] {.59f, .59f, .59f, 0, 0},
                             new[] {.11f, .11f, .11f, 0, 0},
                             new float[] {0, 0, 0, 1, 0},
                             new float[] {0, 0, 0, 0, 1} });

    public static Bitmap MakeGrayscale(Image original, int percent = 100)
    {
        ColorMatrix matrix = GetMatrix(percent); // returns a variation of the above.

        //create a blank bitmap the same size as original
        var newBitmap = new Bitmap(original.Width, original.Height);

        //get a graphics object from the new image
        var g = System.Drawing.Graphics.FromImage(newBitmap);

        //create some image attributes
        var attributes = new ImageAttributes();

        //set the color matrix attribute
        attributes.SetColorMatrix(matrix);

        //draw the original image on the new image
        //using the grayscale color matrix
        g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
           0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

        //dispose the Graphics object
        g.Dispose();
        return newBitmap;
    }

所以问题是两个部分。首先,我可以为此使用矩阵吗?如果是,我有什么不同,如果不是,什么是可行的方法?我正在考虑创建全灰度,然后将其与彩色图像合并(以某种方式)。

谢谢

编辑:原文开启代码

4

1 回答 1

1

要更改饱和度,在http://www.codeproject.com/Tips/78995/Image-colour-manipulation-with-ColorMatrix上有一篇有趣的文章

于 2012-06-10T08:22:44.187 回答