3

In Photoshop, we can change the Brightness of image within the range of -150 to 150.

Using PhotoshopCS4: Image → Adjustments → Brightness/Contrast.

We set this level to 30 for a particular image (A.jpg) and save the image (A_30Bright.jpg)

Used same input for C# application, where we have normalized the value. Referred this: https://stats.stackexchange.com/questions/178626/how-to-normalize-data-between-1-and-1

Our Normalized function looks like below

public static float GetNormalizedValue(float OldValue)
    {
        float OldMax = 150, OldMin = -150, NewMax = 255F, NewMin = -255F;

        float OldRange = (OldMax - OldMin);
        float NewRange = (NewMax - NewMin);
        float NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin;

        return NewValue;
    }

Below is sample code for this using color Matrix

public static double ChangeImageBrightness(string inputImagePath, float value)
    {
        imagePath = inputImagePath;
        Bitmap _bmp = new Bitmap(inputImagePath);

        float FinalValue = GetNormalizedValue(value);

        FinalValue = FinalValue / 255.0f;

        float[][] ptsArray ={
            new float[] { 1.0f, 0, 0, 0, 0},
            new float[] {0, 1.0f, 0, 0, 0},
            new float[] {0, 0, 1.0f, 0, 0},
            new float[] {0, 0, 0, 1.0f, 0},
            new float[] { FinalValue, FinalValue, FinalValue, 0, 1}};

        ImageAttributes imageAttributes = new ImageAttributes();
        imageAttributes.ClearColorMatrix();
        imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

        Bitmap adjustedImage = new Bitmap(_bmp.Width, _bmp.Height);

        Graphics g = Graphics.FromImage(adjustedImage);
        g.DrawImage(_bmp, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height), 0, 0, _bmp.Width, _bmp.Height, GraphicsUnit.Pixel, imageAttributes);

        adjustedImage.Save(Path.GetFileName(imagePath)); }

The Result image from PhotoShop and from C# .Net are not matching.

Brightness in C# is having more shades of white in it.

e.g. Original Image

enter image description here

C# Brightness (Color-matrix Factor=0.2) [+30] enter image description here

Photoshop Brightness [+30] enter image description here

How can we achieve same result as Photoshop from C#.

Banng
  • 531
  • 1
  • 6
  • 19

0 Answers0