Saturday, January 8, 2011

Change Brightness of Image Realtime


public static Bitmap AdjustBrightness(Bitmap Imageint Value)
        {
           System.Drawing.Bitmap TempBitmap = Image;
            float FinalValue = (float)Value / 255.0f;
           System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
           System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
             float[][] FloatColorMatrix ={
                     new float[] {1, 0, 0, 0, 0},
                     new float[] {0, 1, 0, 0, 0},
                     new float[] {0, 0, 1, 0, 0},
                     new float[] {0, 0, 0, 1, 0},
                     new float[] {FinalValue, FinalValue, FinalValue, 1, 1}
                };

           System.Drawing.Imaging.ColorMatrix NewColorMatrix = newSystem.Drawing.Imaging.ColorMatrix(FloatColorMatrix);
           System.Drawing.Imaging.ImageAttributes Attributes = newSystem.Drawing.Imaging.ImageAttributes();
           Attributes.SetColorMatrix(NewColorMatrix);
             NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel, Attributes);
           Attributes.Dispose();
            NewGraphics.Dispose();
            return NewBitmap;
        }

No comments:

Post a Comment