Advertisement
Guest User

Untitled

a guest
Sep 25th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Drawing;
  7. using System.Drawing.Text;
  8. using System.Drawing.Drawing2D;
  9. using System.Drawing.Imaging;
  10.  
  11.  
  12. namespace ASCIITools
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             Console.WriteLine("Would you like to make a [C]olor or [B]&W image?");
  19.             char c = ' ';
  20.             bool isvalid = Char.TryParse(Console.ReadLine(), out c);
  21.             if (isvalid && (c == 'C' || c == 'c') | (c == 'B' || c== 'b'))
  22.             {
  23.                 Console.WriteLine("Please select a path to gather an image");
  24.                 string path = Console.ReadLine();
  25.  
  26.                 if (File.Exists(path))
  27.                 {
  28.                     Console.WriteLine("Choose a name for new image");
  29.                     string name = Console.ReadLine();
  30.  
  31.                     Console.WriteLine("Please select a directory to save the image to");
  32.                     string save = Console.ReadLine();
  33.                     Console.WriteLine("Saving Image...");
  34.                     try
  35.                     {
  36.                         var picture = Image.FromFile(path);
  37.                         if ((c == 'C' || c == 'c'))
  38.                             ImageConverter.ConvertImage(picture, save + "\\" + name + ".png", true);
  39.                         if ((c == 'B' || c == 'b'))
  40.                             ImageConverter.ConvertImage(picture, save + "\\" + name + ".png", false);
  41.                        
  42.                         Console.WriteLine("Image Saved!");
  43.                         Main(new string[0]);
  44.                     }
  45.                     catch (Exception ex)
  46.                     {
  47.                         Error(new Exception(ex.Message + System.Environment.NewLine + "Generally invalid post path specification. i.e. C: instead of C:\\"));
  48.                     }
  49.                 }
  50.                 else
  51.                     Error(new Exception("Invalid path."));
  52.             }
  53.             else
  54.                 Error(new Exception("Invalid decision."));
  55.         }
  56.  
  57.         static void Error(Exception ex)
  58.         {
  59.             Console.WriteLine(String.Format("Error: {0}", ex.Message));
  60.             Main(new string[0]);
  61.         }
  62.     }
  63.  
  64.     public static class ImageConverter
  65.     {
  66.         public static void ConvertImage(Image img, string path, bool isColor)
  67.         {
  68.             int ratio = img.Width / img.Height;
  69.             Image toDraw = new Bitmap(img.Width * 10, img.Height * 10);
  70.             Graphics draw = Graphics.FromImage(toDraw);
  71.             draw.Clear(Color.White);
  72.             draw.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
  73.             Font font = new Font(FontFamily.GenericSansSerif, 10);
  74.  
  75.             Bitmap bmp = new Bitmap(img);
  76.             for (int y = 0; y < bmp.Height; y++)
  77.             {
  78.                 for (int x = 0; x < bmp.Width; x++)
  79.                 {
  80.                     Color color = bmp.GetPixel(x, y);
  81.                     int grey = ConvertToGrayScale(color);
  82.                     Color colorGreyScale = Color.FromArgb(grey, grey, grey);
  83.                     string toPaint = ASCIIPixel((int)colorGreyScale.R).ToString();
  84.                    
  85.                     Brush text;
  86.                     if (isColor)
  87.                         text = new SolidBrush(color);
  88.                     else
  89.                         text = new SolidBrush(Color.Black);
  90.  
  91.                     draw.DrawString(toPaint, font, text, new PointF((float)x * 10, (float)y * 10));
  92.                     text.Dispose();
  93.                 }
  94.             }
  95.  
  96.             draw.Save();
  97.             draw.Dispose();
  98.             toDraw.Save(path, ImageFormat.Png);
  99.             toDraw.Dispose();
  100.         }
  101.  
  102.         /// <summary>
  103.         /// Can be adjusted as needed.
  104.         /// The idea is to start with the smallest character
  105.         /// as a representation closest to white and vice versa
  106.         /// </summary>
  107.         /// <param name="shade"></param>
  108.         /// <returns></returns>
  109.         private static char ASCIIPixel(int shade)
  110.         {
  111.             char dot = '.';
  112.             if (shade >= 250)
  113.                 return (char)32;    //' '
  114.             else if (shade >= 245)
  115.                 return (char)39;    //.
  116.             else if (shade >= 235)
  117.                 return (char)46;    //'
  118.             else if (shade >= 230)
  119.                 return (char)42;    //"
  120.             else if (shade >= 210)
  121.                 return (char)34;    //*
  122.             else if (shade >= 185)
  123.                 return (char)58;    //:
  124.             else if (shade >= 175)
  125.                 return (char)59;    //;
  126.             else if (shade >= 155)
  127.                 return (char)33;   //?
  128.             else if (shade >= 120)
  129.                 return (char)63;    //!
  130.             else if (shade >= 85)
  131.                 return (char)37;    //$
  132.             else if (shade >= 10)
  133.                 return (char)56;    //@
  134.             else if (shade >= 0)
  135.                 return (char)35;    //#
  136.             return dot;
  137.         }
  138.  
  139.         public static int ConvertToGrayScale(Color color)
  140.         {
  141.             return (color.R / 3) + (color.G / 3) + (color.B / 3);
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement