Pages

8/07/2012

Browse images from directory and Convert Colored Images to Gray Scale c#

Recently i have to convert colored bitmap/jpeg images to gray scale in my project. I found many solutions on Google but found one useful. This tutorial will help you browse images from directory and convert them to gray scale one by one.

First of all create a solution and windows form project in visual studio.

Then drag two picture boxes from toolbox on the left on to the form and two buttons also.

Name picture boxes pctColoredImage and pctGrayScale. Also the two buttons btnNext and btnPrevious and show text on them Next and Previous respectively.

Double click on each picture boxes and buttons to create their events.

Now create

List<string> FilesFromFolder = null; a global variable and in constructor initialize it like this

FilesFromFolder = new List<string>();

Now in form load add files from the folder to the list just initialized in the constructor.


string[] fileEntries = Directory.GetFiles("data/")
                foreach (string fileName in fileEntries)
                {
                    // add directory files to list
                    FilesFromFolder.Add(fileName);
                }

Declare int fileIndex = 0; as global variable to track count of index.

Now in event of onClick of btnNext write this code


if (FilesFromFolder.Count == fileIndex)
                fileIndex = 0;

            this.Text = FilesFromFolder[fileIndex];
            CalculateACCR(fileIndex);
            pctBoxMain.Image = new Bitmap(FilesFromFolder[fileIndex++]);

            pctBoxGrayScale.Image =  ConvertToGrayscale (new Bitmap(pctBoxMain.Image));

In event of onClick of btnPrevious write this code


if (fileIndex == 0)
                fileIndex = FilesFromFolder.Count;

            this.Text = FilesFromFolder[(fileIndex) - 1];

            CalculateACCR(fileIndex - 1);

            pctBoxMain.Image = new Bitmap(FilesFromFolder[(fileIndex) - 1]);
            fileIndex--;

            pctBoxGrayScale.Image = ConvertToGrayscale(new Bitmap(pctBoxMain.Image));

now to convert images to gray scale us this function


public static Bitmap  ConvertToGrayscale (Bitmap original)
        {
            //create a blank bitmap the same size as original
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);

            //get a graphics object from the new image
            Graphics g = Graphics.FromImage(newBitmap);

            //create the grayscale ColorMatrix
            ColorMatrix colorMatrix = new ColorMatrix(
               new float[][] 
                              {
                                 new float[] {.3f, .3f, .3f, 0, 0},
                                 new float[] {.59f, .59f, .59f, 0, 0},
                                 new float[] {.11f, .11f, .11f, 0, 0},
                                 new float[] {0, 0, 0, 1, 0},
                                 new float[] {0, 0, 0, 0, 1}
                              });

            //create some image attributes
            ImageAttributes attributes = new ImageAttributes();

            //set the color matrix attribute
            attributes.SetColorMatrix(colorMatrix);

            //draw the original image on the new image
            //using the grayscale color matrix
            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
               0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

            //dispose the Graphics object
            g.Dispose();
            return newBitmap;
        }





Note: you may have to adjust slightly the index for next and previous image.


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Favorites More