[C#]Img2ASCII - Open Source

Results 1 to 11 of 11
  1. #1
    Account Upgraded | Title Enabled! Intelext is offline
    MemberRank
    Mar 2007 Join Date
    EuropeLocation
    1,228Posts

    [C#]Img2ASCII - Open Source

    Img2ASCII
    Remember that the better quality is your image, the better quality will be result.

    Mission:
    Create application that makes ASCII art from pictures.

    How does it work:
    Press "Convert" button, open your image, save your HTML file. Default width symbols are 200 and it is optimal symbol count for quality. You can choose different width symbol count yourself.
    It's taking every your image, makes it black and white. Then it applies symbols from black to white.

    Screenshot:
    http://img213.imageshack.us/img213/460/screenrf.png

    Virus Check:
    http://www.virustotal.com/analisis/5...267-1263833816


    Source Code

    (I won't share my project files. All you need is here.)

    Main Form
    PHP Code:
    #region Using...
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Img2ASCIIConverter.Classes;
    using System.Threading;
    using System.IO
    #endregion

    namespace Img2ASCIIConverter
    {
        public 
    partial class frmMainForm Form
        
    {
            
    //Global variables.
            
    StringBuilder ASCIICode null;
            
    int ImageQuality;

            public 
    frmMainForm()
            {
                
    InitializeComponent();
            }

            private 
    void buttonConvert_Click(object senderEventArgs e)
            {
                
    ImageQuality trackBarQuality.Value;
                
    openFileDialogImage.ShowDialog();

                
    //Creating our thread so that it won't lag. (Just in case)
                
    Thread ConvertingThread = new Thread(StartConverting);
                
    //ApartmentState = STA. Because we need to open file save dialogue after thread is done.
                
    ConvertingThread.SetApartmentState(ApartmentState.STA);
                
    //Starting our thread! :)
                
    ConvertingThread.Start();
            }

            private 
    void StartConverting()
            {
                
    //We TRY because some errors might appear if we select MP3 file for example.
                
    try
                {
                    
    Bitmap bmp = new Bitmap(openFileDialogImage.FileName);
                    
    ASCIICode = new StringBuilder();

                    
    //We start the class method but first we convert our image to our desierd quality (size).
                    
    ASCIICode.Append(Img2ASCII.Convert(Img2ASCII.GetReSizedImage(bmpImageQuality)));
                    
    //Annoying message showing that we succeeded. :)
                    
    MessageBox.Show("Converting process done!""Success"MessageBoxButtons.OKMessageBoxIcon.Information);
                    
    //Opening our save dialog to save our HTML file.
                    
    saveFileDialogASCIICode.ShowDialog();
                }
                
    //Don't want anything to be shown, but still it REQ to catch something 0.o
                
    catch
                {

                }
            }

            private 
    void saveFileDialogASCIICode_FileOk(object senderCancelEventArgs e)
            {
                
    //Saving and closing...
                
    TextWriter ASCIIFile = new StreamWriter(saveFileDialogASCIICode.FileName);
                
    ASCIIFile.Write(ASCIICode.ToString());
                
    ASCIIFile.Close();
            }
        }

    Img2ASCII.cs
    PHP Code:
    /*
     ************************************************************************************
     * Class that converts PICTURES to ASCII code.                                      *
     * by Steve Moss (Qmal)                                                             *
     * Dr.Qmal@gmail.com                                                                *
     *                                                                                  *
     * Some code parts are taken from MSDN. Some are improved. Not 100% my work.        *
     * With Love, From Qmal.                                                            *
     ************************************************************************************                                                                                  
     */

    #region Using...
    using System;
    using System.Text;
    using System.Drawing;
    using System.Windows.Forms
    #endregion

    namespace Img2ASCIIConverter.Classes
    {
        class 
    Img2ASCII
        
    {
            public static 
    string Convert(Image img)
            {
                
    //Our String Builder and Bitmap.
                
    StringBuilder html = new StringBuilder();
                
    Bitmap bmp null;

                try
                {
                    
    //Creating new Bitmap out of our Image.
                    
    bmp = new Bitmap(img);
                    
                    
    //Setting font size to smallest and fonts to Terminal. (HTML Code)
                    
    html.Append("<font size='1' face='Terminal'>");

                    
    //Going through every pixel in image.
                    //Making whole image Black&White.
                    //I could use Matrix method which is faster and better, but I'm not so good at Mathematics (Junior) so I pass.
                    
    for (int y 0bmp.Heighty++)
                    {
                        for (
    int x 0bmp.Widthx++)
                        {
                            
    //Getting the color of current pixel.
                            
    Color col bmp.GetPixel(xy);

                            
    //Making our image Black&White
                            
    col Color.FromArgb((col.col.col.B) / 3, (col.col.col.B) / 3, (col.col.col.B) / 3);

                            
    //Brightness Value.
                            
    int bValue col.R;

                            
    //Applying ASCII character.
                            
    html.Append(getASCIIChar(bValue));

                            
    //If our image width pixels are over we are making a break line in text.
                            
    if (== bmp.Width 1)
                                
    html.Append("</br>");
                        }
                    }
                    
                    
    //Finnaly I can close off HTML code.
                    
    html.Append("</font>");

                    
    //Returning our ASCII drawing.
                    
    return html.ToString();
                }

                catch (
    Exception e)
                {
                    
    //Showing error message if something went wrong...
                    
    MessageBox.Show("Something went wrong...""Critical Error"MessageBoxButtons.OKMessageBoxIcon.Error); 
                    return 
    e.ToString();
                }

                finally
                {
                    
    //Disposing our image.
                    
    bmp.Dispose();
                }
            }

            private static 
    string getASCIIChar(int bValue)
            {
                
    //Simple stuff here.
                //Assinging characters to level of Brightness of pixel.
                
    string ASCIIChar;

                if (
    bValue >= 230)
                {
                    
    ASCIIChar " ";
                }
                else if (
    bValue >= 200)
                {
                    
    ASCIIChar ".";
                }
                else if (
    bValue >= 180)
                {
                    
    ASCIIChar ",";
                }
                else if (
    bValue >= 160)
                {
                    
    ASCIIChar ":";
                }
                else if (
    bValue >= 130)
                {
                    
    ASCIIChar "o";
                }
                else if (
    bValue >= 100)
                {
                    
    ASCIIChar "&";
                }
                else if (
    bValue >= 70)
                {
                    
    ASCIIChar "8";
                }
                else if (
    bValue >= 50)
                {
                    
    ASCIIChar "#";
                }
                else
                {
                    
    ASCIIChar "@";
                }

                return 
    ASCIIChar;
            }

            public static 
    Bitmap GetReSizedImage(Bitmap inputBitmapint asciiWidth)
            {
                
    //Converting our image.
                //I could use some other methods without converting image, but this is easiest I think.
                //I took this frome somewhere.
                //MSDN Probably. :)

                //Calculate the new Height of the image from its width
                
    int asciiHeight = (int)Math.Ceiling((double)inputBitmap.Height asciiWidth inputBitmap.Width);

                
    //Create a new Bitmap and define its resolution
                
    Bitmap result = new Bitmap(asciiWidthasciiHeight);

                
    Graphics g Graphics.FromImage((Image)result);

                
    //The interpolation mode produces high quality images
                
    g.InterpolationMode System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                
    g.DrawImage(inputBitmap00asciiWidthasciiHeight);
                
    g.Dispose();

                return 
    result;
            }

        }

    Attached Files Attached Files
    Last edited by Intelext; 19-01-10 at 05:05 PM.


  2. #2
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [C#]Img2ASCII - Open Source

    Hey, that's cool!

    I saw one of these that did full color ASCII font. It also let you pick black+white, Does the "Matrix" way of doing it let you do full color?

    Also, the part you said that was "simple stuff", is really the genius of the whole operation.

    Nicely done ;)

  3. #3
    The Cat in the Hat cypher is offline
    MemberRank
    Oct 2005 Join Date
    IrelandLocation
    5,073Posts

    Re: [C#]Img2ASCII - Open Source

    Nice one, i like it, is it possible to make it colored ascii too?

  4. #4
    The Omega Superfun is offline
    MemberRank
    Dec 2006 Join Date
    The NetherlandsLocation
    5,227Posts

    Re: [C#]Img2ASCII - Open Source

    Code:
    html.Append("<font size='1' face='Terminal'");
    not closing the font tag (>)

  5. #5
    Account Upgraded | Title Enabled! Intelext is offline
    MemberRank
    Mar 2007 Join Date
    EuropeLocation
    1,228Posts

    Re: [C#]Img2ASCII - Open Source

    Quote Originally Posted by Superfun View Post
    Code:
    html.Append("<font size='1' face='Terminal'");
    not closing the font tag (>)
    Yea, but hey! It's still working as intended.

    Yes, it is possible to make it colored. Remember R,G,B values? You can arrange your symbol color based on those. :)

    Of course the process will be more slower then but it's not a problem.

  6. #6
    The Omega Superfun is offline
    MemberRank
    Dec 2006 Join Date
    The NetherlandsLocation
    5,227Posts

    Re: [C#]Img2ASCII - Open Source

    nah it didnt worked for me untill i did that since it broke the first few lines due of it

  7. #7
    Account Upgraded | Title Enabled! Intelext is offline
    MemberRank
    Mar 2007 Join Date
    EuropeLocation
    1,228Posts

    Re: [C#]Img2ASCII - Open Source

    Quote Originally Posted by Superfun View Post
    nah it didnt worked for me untill i did that since it broke the first few lines due of it
    Maybe this is why it sometimes bugs out. Altho very unlikely. Thanks for pointing out tho.

    Fixed it. :)
    Last edited by Intelext; 19-01-10 at 05:05 PM.

  8. #8
    Proficient Member Zinoxity is offline
    MemberRank
    Dec 2009 Join Date
    198Posts

    Re: [C#]Img2ASCII - Open Source

    Wow, great job, I never thought you could do this.

  9. #9
    Music<3 Katsuro is offline
    MemberRank
    Oct 2006 Join Date
    Kailua-Kona, HILocation
    1,051Posts

    Re: [C#]Img2ASCII - Open Source

    Cool shit. Great work.

  10. #10
    Apprentice Matth3w is offline
    MemberRank
    Mar 2010 Join Date
    7Posts

    Re: [C#]Img2ASCII - Open Source

    This is really a great tool and has helped me a fair bit!

    Thank you very much!

  11. #11
    akakori akakori is offline
    MemberRank
    Apr 2008 Join Date
    613Posts

    Re: [C#]Img2ASCII - Open Source

    there is a way to do it..

    one way would be, to make a switch with a comparison. Based on the RGB value check which one is higher, based on which on is higher make it process a switch.

    instead of this:
    return ASCIIChar;

    do smth like this
    return "<font size='1' face='Terminal' color='red'>"+ASCIIChar+"</font>";



Advertisement