namespace _4_Convert_To_Greyscale
{
    using System;
    using System.Drawing;
    using System.IO;

    public class Program
    {
        public static void Main(string[] args)
        {
            const string fileName = @"C:\Users\Stefan\Downloads\john-romero.jpg";

            Console.WriteLine("Converting file: {0}\n", fileName);
            ConverteToGrayScale(fileName);
        }

        public static void ConverteToGrayScale(string imagePath)
        {
            var bitmap = new Bitmap(imagePath);

            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    Color color = bitmap.GetPixel(i, j);
                    int grayScale = (int)((color.R * 0.3) + (color.G * 0.59) + (color.B * 0.11));
                    Color newColor = Color.FromArgb(color.A, grayScale, grayScale, grayScale);
                    bitmap.SetPixel(i, j, newColor);
                    if (i % 1000 == 0)
                    {
                        Console.Write(".");                        
                    }
                }
            }
            var fileName = Path.GetFileNameWithoutExtension(imagePath) + "_grayScale";
            var newPath = Path.Combine(Path.GetDirectoryName(imagePath), fileName + Path.GetExtension(imagePath));
            var stream = new FileStream(newPath, FileMode.OpenOrCreate);
            bitmap.Save(stream, bitmap.RawFormat);

            Console.WriteLine("\nDone. Image saved at: {0}", newPath);
        }
    }
}