Change the contrast of Image like Adobe Photoshop in ASP.net C#

Posted by Hoque on Stack Overflow See other posts from Stack Overflow or by Hoque
Published on 2010-04-07T02:17:28Z Indexed on 2010/04/07 2:23 UTC
Read the original article Hit count: 604

While I was trying to change the brightness and contrast of Image using C#, but I could not get success in changing the contrast of the Image. May I expect any support from here.

I am using ColorMatrix to do that.

Here are the code that I am using for brightness(works fine) and contrast(does not work properly).

public static ColorMatrix CreateBrightnessMatrix(float Brightness)
{
    if (Brightness < -1 || Brightness > 1)
        throw new ArgumentOutOfRangeException("Brightness value is out of range");

    ColorMatrix cm = new ColorMatrix(new float[][]{
                        new float[] {        1f,          0,          0,  0,  0},
                        new float[] {         0,         1f,          0,  0,  0},
                        new float[] {         0,          0,         1f,  0,  0},
                        new float[] {         0,          0,          0, 1f,  0},
                        new float[] {Brightness, Brightness, Brightness, 1f, 1f}});

    return cm;
}

public static ColorMatrix CreateContrastMatrix(float Contrast)
{
    if (Contrast < 0 || Contrast > 3)
        throw new ArgumentOutOfRangeException("Contrast value is out of range");

    float Trans = (1f - Contrast) / 2f;

    ColorMatrix cm = new ColorMatrix(new float[][]{
                        new float[] {Contrast,       0f,       0f, 0f, 0f},
                        new float[] {      0f, Contrast,       0f, 0f, 0f},
                        new float[] {      0f,       0f, Contrast, 0f, 0f},
                        new float[] {      0f,       0f,       0f, 1f, 0f},
                        new float[] {   Trans,    Trans,    Trans, 0f, 1f}});

    return cm;
}

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET