Friday, January 31, 2014

Convert Color to gray scale in C# ( C sharp )



This is my post related with C # (C sharp). In this post, I will explain the steps of making the application of C #. Application creation Tutorial Convert color to gray is part of the C # application usage as it relates to the process image. The image will be processed convert of original color to a shade of gray.Now it's time to direct the implementation of the program.
Maybe many of you already know very well about the programming language one, namely how to turn any type color images of the original color to gray scale levels by using the C # programming language. In my implementation using  Microsoft Visual Studio 2012.
Please read:
1. the first step, open the C # program and create a new file by choosing File > > New Project (Ctrl + Shift + N) > > select Windows Application, and name the file as per your liking (for example ConvertGrayscale) and press OK. Next would come an empty form.

2. a Component used for this simple program is :
     • OpenFileDialog
     • PictureBox
     • Button
Click and put option component of the program which will be used as the image below :
3. Then double click on the button "Load" and add the following code :

OpenFileDialog file = new OpenFileDialog();
string lokasi = file.InitialDirectory;
lokasi = @"C:\Users\ardha\Desktop";
file.Title = "Open Image File";
file.Filter = "Image Files(*.jpg,*.png,*.tiff,*.bmp,*.gif)|*.jpg;*.png;*.tiff;*.bmp;*.gif";
file.FilterIndex = 2;
file.RestoreDirectory = true;
if (file.ShowDialog() == DialogResult.OK)
                     {
                     pictureBox1.Image = new Bitmap(file.FileName);
                                     } 
The purpose of the button loads the image file is to choose what you'd like displayed in the picture box. The desired file types can be specified via the syntax Filter (in the above example file.Filter), whereas if you want to directly refer to a particular directory can be used syntax InitialDirectory (in the above example file.InitialDirectory)

4. whereas, to change the image file onto gray scale, double click on the button "Convert" and add the following code :

            Gray = new Bitmap(pictureBox1.Image);
            pictureBox1.Image = MakeGrayscale(Gray);
Here we need a declaration to "MakeGrayscale" MakeGrayscale code, as follows :

            Bitmap newBitmap = new Bitmap(original.Width, original.Height);

            for (int i = 0; i < original.Width; i++)
            {
                for (int j = 0; j < original.Height; j++)
                {
                    Color originalColor = original.GetPixel(i, j);
                    int grayScale = (int)((originalColor.R * .3) + (originalColor.G *
                    .59) + (originalColor.B * .11));
                    Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);
                    newBitmap.SetPixel(i, j, newColor);
                }
            }
            return newBitmap; 

5. the application was complete, after adding the source code above, lives in the run course. And the results were as follows :

For more details on this please downlaod program understanding program files and full-length project

download program files here

download full project here

download the exe only here
download the document here
download picture here

for more information contact on ardhamail@gmail.com

Guide to download: click the download link provided above, a page will appear after that adf.ly, wait a few seconds until appears on the top right corner of the ad skip writing. Click the button, the page will appear after that ziddu, please click on the download button that appears on the page ziddu
NB: in this tutorial program using the Visual studio IDE. with the target framework > 4.0. So at a minimum your computer must be installed or windows vista or windows xp with the .NET framework 4.0 to be able to run programs *. exe, while for open this project inevitably must install microsoft visual studio 2012
 

0 comments:

Post a Comment