Symmetri Developer Blog

January 22, 2008

Adjust image brightness in C#

.NET/C#, Algorithms - By Shourov Bhattacharya

There are a couple of ways to do this. This method does not use ColorMatrix.


private static System.Drawing.Image _AlterBrightness(System.Drawing.Image bmp, int level)
{
   Graphics graphics = Graphics.FromImage(bmp);
    if (level == 50)
    {
       // do nothing
       return bmp;
    }
   else if (level <50)
    {
       // make it darker
       // Work out how much darker
       int conversion = 250 - (5 * level);
       Pen pDark = new Pen(Color.FromArgb(conversion,0,0,0), bmp.Width*2);
       graphics.DrawLine(pDark,0,0,bmp.Width,bmp.Height);
    }
    else if (level >50)
    {
       // mark it lighter
       // Work out how much lighter.
       int conversion = (5 * (level - 50));
       Pen pLight = new Pen(Color.FromArgb(conversion,255,255,255), bmp.Width*2);
       graphics.DrawLine(pLight,0,0,bmp.Width,bmp.Height);
    }
    graphics.Save();
    graphics.Dispose();
    return bmp;
}

Comments »

No comments yet.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Janis Joseph