Take a bitmap and lighten it by a given percentage - CSharp System.Drawing

CSharp examples for System.Drawing:Bitmap

Description

Take a bitmap and lighten it by a given percentage

Demo Code

/* Copyright (c) 2006-2008, Peter Golde
 * All rights reserved.//  w  w  w.j  av a 2 s.  c  om
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are 
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of Peter Golde, nor "Purple Pen", nor the names
 * of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
using System.Diagnostics;
using System;

public class Main{
        // Take a bitmap and lighten it by a given percentage. 0.0 takes it all the way to white, 1.0 leaves it unchanched, 0.5 lightens
        // it by half.
        public static void LightenBitmap(Bitmap bm, double lightenFactor)
        {
            int height = bm.Height;
            int width = bm.Width;
            BitmapData bmData = bm.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            byte[] lightenFactors = CalculateLightening(lightenFactor);

            unsafe {
                byte* pixelData = ((byte*) bmData.Scan0);

                fixed (byte* lookup = lightenFactors) {
                    for (int i = 0; i < height; ++i) {
                        AdjustBits(pixelData, 3 * width, lookup);

                        pixelData += bmData.Stride;
                    }
                }
            }

            bm.UnlockBits(bmData);
        }
        // Return a byte lookup table to lighten the red, green, or blue part of a color.
        static byte[] CalculateLightening(double lightenFactor)
        {
            byte[] lookupTable = new byte[256];

            for (int i = 0; i < 256; ++i) {
                lookupTable[i] = (byte) Math.Round(255 - ((255 - i) * lightenFactor));
            }

            return lookupTable;
        }
}

Related Tutorials