Click here to Skip to main content
15,888,968 members
Articles / Programming Languages / C#

ASCII Imaging

Rate me:
Please Sign up or sign in to vote.
4.93/5 (6 votes)
2 Jan 2013CPOL2 min read 25.2K   516   22  
How to convert images to ASCII in a Console Application with colors.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Image2ASCII
{
    class Program
    {
        static Color[] ConsoleColors()
        {
            Color[] array = new Color[16];
            string[] names = Enum.GetNames(typeof(ConsoleColor));
            for (int i = 0; i < names.Length; i++)
                array[i] = Color.FromName(names[i]);
            return array;
        }
        static ConsoleColor Closest(Color _1)
        {
            if (_1.IsSystemColor)
                return (ConsoleColor)Enum.Parse(typeof(ConsoleColor), _1.Name);
            double dbl_input_red = Convert.ToDouble(_1.R);
            double dbl_input_green = Convert.ToDouble(_1.G);
            double dbl_input_blue = Convert.ToDouble(_1.B);
            double distance = 500.0;

            double dbl_test_red;
            double dbl_test_green;
            double dbl_test_blue;
            Color nearest_color = Color.Empty;
            foreach (object o in ConsoleColors())
            {
                dbl_test_red = Math.Pow(Convert.ToDouble(((Color)o).R) - dbl_input_red, 2.0);
                dbl_test_green = Math.Pow(Convert.ToDouble
                    (((Color)o).G) - dbl_input_green, 2.0);
                dbl_test_blue = Math.Pow(Convert.ToDouble
                    (((Color)o).B) - dbl_input_blue, 2.0);
                double temp = Math.Sqrt(dbl_test_blue + dbl_test_green + dbl_test_red);
                if (temp == 0.0)
                {
                    nearest_color = (Color)o;
                    break;
                }
                else if (temp < distance)
                {
                    distance = temp;
                    nearest_color = (Color)o;
                }
            }
            return (ConsoleColor)Enum.Parse(typeof(ConsoleColor), nearest_color.Name);

        }
        static string Brightness(Color _1)
        {
            int factor = _1.R + _1.G + _1.B;
            if (factor >= 602)
                return "M";
            if (factor >= 572)
                return "@";
            if (factor >= 532)
                return "#";
            if (factor >= 502)
                return "8";
            if (factor >= 472)
                return "0";
            if (factor >= 432)
                return "&";
            if (factor >= 402)
                return "%";
            if (factor >= 372)
                return "?";
            if (factor >= 332)
                return "<";
            if (factor >= 302)
                return "/";
            if (factor >= 252)
                return "*";
            if (factor >= 200)
                return ".";
            return " ";
        }
        static CString CSimiliarity(Color _1)
        {
            CString str = new CString();
            str.String = Brightness(_1);
            str.Color = Closest(_1);
            return str;
        }
        static CString[,] ImageToASCII(Bitmap b)
        {
            Image.GetThumbnailImageAbort myCallback = null;
            if (b.Width > 80)
                b = (Bitmap)b.GetThumbnailImage(80, 60, myCallback, IntPtr.Zero);
            CString[,] map = new CString[b.Width, b.Height];
            for (int y = 0; y < b.Height; y++)
            {
                for (int x = 0; x < b.Width; x++)
                {
                    map[x, y] = CSimiliarity(b.GetPixel(x, y));
                }
            }
            return map;
        }
        static void PrintCStringMult(CString[,] mult)
        {
            int width = mult.GetLength(0);
            int height = mult.GetLength(1);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    CString m = mult[x, y];
                    Console.ForegroundColor = m.Color;
                    Console.Write(m.String);
                }
            }

        }

        static void Main(string[] args)
        {
            string path = @"C:\example.png";
            CString[,] img = ImageToASCII((Bitmap)Image.FromFile(path));
            PrintCStringMult(img);
            Console.ReadKey();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions