Uses an RGB histogram to equalize the image - CSharp System.Drawing

CSharp examples for System.Drawing:Image Effect

Description

Uses an RGB histogram to equalize the image

Demo Code

/*//from  ww  w .j ava  2s  .  c  o m
Copyright (c) 2009 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing;
using System;

public class Main{
        /// <summary>
        /// Uses an RGB histogram to equalize the image
        /// </summary>
        /// <param name="Image">Image to manipulate</param>
        public static Bitmap Equalize(Bitmap Image)
        {
            System.Drawing.Bitmap TempBitmap = Image;
            System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
            System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
            NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
            NewGraphics.Dispose();
            RGBHistogram TempHistogram = new RGBHistogram(NewBitmap);
            TempHistogram.Equalize();
            for (int x = 0; x < NewBitmap.Width; ++x)
            {
                for (int y = 0; y < NewBitmap.Height; ++y)
                {
                    Color Current=NewBitmap.GetPixel(x,y);
                    int NewR = (int)TempHistogram.R[Current.R];
                    int NewG = (int)TempHistogram.G[Current.G];
                    int NewB = (int)TempHistogram.B[Current.B];
                    if (NewR >= 256)
                        NewR = 255;
                    if (NewG >= 256)
                        NewG = 255;
                    if (NewB >= 256)
                        NewB = 255;
                    NewBitmap.SetPixel(x, y, Color.FromArgb(NewR, NewG, NewB));
                }
            }
            return NewBitmap;
        }
        /// <summary>
        /// Uses an RGB histogram to equalize the image
        /// </summary>
        /// <param name="FileName">Image to manipulate</param>
        public static Bitmap Equalize(string FileName)
        {
            if (!IsGraphic(FileName))
                return new Bitmap(1, 1);
            System.Drawing.Image TempImage = System.Drawing.Image.FromFile(FileName);
            System.Drawing.Bitmap TempBitmap = new System.Drawing.Bitmap(TempImage, TempImage.Width, TempImage.Height);
            System.Drawing.Bitmap ReturnBitmap = Image.Equalize(TempBitmap);
            TempBitmap.Dispose();
            TempImage.Dispose();
            return ReturnBitmap;
        }
        /// <summary>
        /// Uses an RGB histogram to equalize the image
        /// </summary>
        /// <param name="FileName">Image to manipulate</param>
        /// <param name="NewFileName">Location to save the image to</param>
        public static void Equalize(string FileName, string NewFileName)
        {
            if (!IsGraphic(FileName))
                return;
            ImageFormat FormatUsing = GetFormat(NewFileName);
            System.Drawing.Bitmap NewBitmap = Image.Equalize(FileName);
            NewBitmap.Save(NewFileName, FormatUsing);
            NewBitmap.Dispose();
        }
}

Related Tutorials