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.


6/01/2012

HP g6 weak wireless issue resolved

I recently bought HP g6 i5 and very first day got issue of weak wireless signals. First i thought its  just the router issue but it kept as it is for week or so. 


Got frustrated with it and started googling it. Read some different posts so gonna conclude them.


One post said remove the back cover and check if wireless antennas are not connected to the wireless card. This may be one of the issue. It resolved the issue of the person who posted it.


Another said that just update the wlan broadcomm drivers. It can be done by going into the device manager and clicking expand on the network adapters. Find wireless there and right click on it and update drivers. It works on windows 7. You can also download the drivers from hp site. 


Both are the solutions. I followed the second one n got my laptops signal strength to maximum.


Also there is an option in device manager about the wireless signal. Do check it if its 100% or not.


If none of above works then pray your laptop is in warranty :)


4/10/2012

Use Morse Code to write text in Desktop Apps

First of all create a windows form project in visual studio and drag three buttons on the form and a textbox(you can add further controls but these are minimum controls needed).

Declare following global variables.\

private string inputString = "";
        private int count = 0;

        private static readonly Dictionary<string, string> mapping = new Dictionary<string, string>() {
        {  ".-","a" },
        {  "-...","b" },
        {  "-.-.", "c"},
        {  "-..","d" },
        {  "." ,"e"},
        {  "..-.","f" },
        {  "--.","g" },
        {  "....","h" },
        {  "..","i" },
        {  ".---","j" },
        {  "-.-" ,"k"},
        {  ".-.." ,"l"},
        {  "--" ,"m"},
        {  "-." ,"n"},
        {  "---" ,"o"},
        {  ".--." ,"p"},
        {  "--.-" ,"q"},
        {  ".-." ,"r"},
        {  "..." ,"s"},
        {  "-" ,"t"},
        {  "..-" ,"u"},
        {  "...-" ,"v"},
        {  ".--" ,"w"},
        {  "-..-" ,"x"},
        {  "-.--" ,"y"},
        {  "--.." ,"z"},
        {  "-----" ,"0"},
        {  ".----" ,"1"},
        {  "..---" ,"2"},
        {  "...--" ,"3"},
        {  "....-" ,"4"},
        {  "....." ,"5"},
        {  "-...." ,"6"},
        {  "--..." ,"7"},
        {  "---.." ,"8"},
        {  "----." ,"9"}
    };


Now name one button as "." and second as "-" as morse code depends on these two characters.

Name the third button as "space" which will be used to differentiate characters and letters.

On click event of button named "." write following line.

inputString += ".";

On click event of button named "-" write following line.

inputString += "-";

On click event of button named "space" write following lines. 

            count++;
            if (count == 1 && inputString != "")
            {
                textBox1.Text += GetLetter(inputString);
                count = 0;
            }
            else
            {
                textBox1.Text += GetLetter(inputString) + " ";
                count = 0;
            }

            inputString = "";


        public string GetLetter(string morseCode)
        {
            return mapping.ContainsKey(morseCode.ToString()) ? mapping[morseCode] : "";
        }


 Now build and run this project. 

If you want to differentiate between characters hit space once and if you want to add space hit space twice.

This can b very useful in mobile development where it could be used for typing.

Any suggestions and queries are welcome.


Twitter Delicious Facebook Digg Favorites More