Change the contrast of Image like Adobe Photoshop in ASP.net C#
- by Hoque
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.