Microsoft MVP성태의 닷넷 이야기
.NET Framework: 498. C#으로 간단하게 만들어 본 ASCII Art 프로그램 [링크 복사], [링크+제목 복사],
조회: 16589
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

C#으로 간단하게 만들어 본 ASCII Art 프로그램

문자열로만 만드는 이모티콘 모음 사이트가 있군요. ^^

1 Line Art - ASCII art in one line
; http://1lineart.kulaone.com/

이 사이트를 보니, 예전에 ASCII 코드로 그리는 ascii art에 대해 동료들과 함께 이야기했던 것이 생각났습니다. 그러니까, 사용자가 "i"라고 입력하면 화면에 다음과 같이 보여주는 것이죠.

                   iiiii                        
                  iiiiiii                       
                 iiiiiiii                       
                 iiiiiiii                       
                  iiiiiii                       
                   iiiii                        
                                                
                                                
                                                
            iiiiiiiiiiiii                       
            iiiiiiiiiiiii                       
            iiiiiiiiiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
                   iiiiii                       
           iiiiiiiiiiiiiiiiiiiii                
           iiiiiiiiiiiiiiiiiiiii                
           iiiiiiiiiiiiiiiiiiiii      

제가 그때 낸 의견은 이렇습니다. 'i'라는 글자를 입력하면 이미지로 i를 그린 다음, 그 이미지 전체 영역의 pixel 값을 하나씩 차례로 읽어서 0이면 공백 문자를, 1이면 문자를 출력하면 되지 않겠냐고!

생각난 김에, ^^ 실제로 한번 구현을 해봤습니다. 생각 자체가 간단하기 때문에 코드도 무척 간단합니다.

string GetAsciiArt(char ch)
{
    string text = ch.ToString();

    // 적당한 이미지 크기를 선정하고,
    int width = 48;
    int height = 55;
    Bitmap bitmap = new Bitmap(width, height);

    // Bitmap을 대상으로 그림을 그릴 수 있도록 Graphics 개체를 구한 후,
    Graphics g = Graphics.FromImage(bitmap);

    // 이미지의 바탕을 흰색으로 초기화하고,
    g.Clear(Color.White);

    // 글자를 그립니다.
    RectangleF rect = new RectangleF(0, 0, width, height);
    g.DrawString(text, new Font("Consolas", 48, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.Black, rect);
    g.Flush();

    StringBuilder sb = new StringBuilder();

    // Bitmap에 그려진 모든 pixel을 열람해서,
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            Color clr = bitmap.GetPixel(x, y);

            // 흰색 픽셀이면 공백을 출력하고,
            if (clr.ToArgb() == Color.White.ToArgb())
            {
                sb.Append(" ");
            }
            else // 검정색 픽셀이면 글자를 출력.
            {
                sb.Append(text);
            }
        }

        sb.Append(Environment.NewLine);
    }
    
    return sb.ToString();
}

좀 더 깨끗한 결과를 얻기 위해 위의 코드를 약간 조정해 주고 실행하면 다음과 같이 ^^ 멋진 Ascii art 프로그램이 탄생합니다.

ascii_art_1.png

(첨부한 파일은 위의 프로그램에 대한 소스코드를 포함합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 7/10/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2019-03-04 01시39분
1 LINE ART
; http://1lineart.kulaone.com

PixiEditor/PixiEditor - PixiEditor is a lightweight pixel art editor made with .NET 7
; https://github.com/PixiEditor/PixiEditor

2ascii: Render JPGs PNGs, SVGs and text to ASCII
; https://www.codeproject.com/Articles/5369578/2ascii-Render-JPGs-PNGs-SVGs-and-text-to-ASCII
정성태
2023-04-17 09시19분
https://twitter.com/PR0GRAMMERHUM0R/status/1645622724033753091/photo/1

저도 사실 습관처럼 쓰지만 ^^; 엄밀히 ASCII 인코딩은 7-bit 인코딩이고 근래에는 거의 사용하지 않고 있습니다. 현재 우리가 흔히 일컫는 ASCII는 실제로는 ISO-8859 표준으로 나온 8-bit 인코딩에 해당합니다.
정성태

... 61  62  63  64  65  66  67  68  [69]  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
11904정성태5/21/201916641.NET Framework: 833. C# - Open Hardware Monitor를 이용한 CPU 온도 정보 [1]파일 다운로드1
11903정성태5/21/201911681오류 유형: 540. .NET Core - System.PlatformNotSupportedException: The named version of this synchronization primitive is not supported on this platform.
11902정성태5/21/201910860오류 유형: 539. mstest 실행 시 "The directory name is invalid." 오류 발생
11901정성태5/21/201911994오류 유형: 538. msbuild 오류 - Could not find a part of the path '%LOCALAPPDATA%\Temp\2\.NETFramework,Version=v4.0.AssemblyAttributes.cs'
11900정성태5/18/201911288오류 유형: 537. "sfc /scannow" 실행 중 시스템이 부팅되는 현상
11899정성태5/17/201912268Linux: 9. Linux에서 윈도우의 OutputDebugString 대신 사용할 수 있는 syslog [1]
11898정성태5/16/201913633VC++: 130. C++ string의 c_str과 data 함수의 차이점 [3]
11897정성태5/16/201920226오류 유형: 536. Visual Studio - "Developer Pack"을 설치했는데도 "대상 프레임워크" 목록에 나오지 않는 경우 [2]
11896정성태5/15/201914998개발 환경 구성: 440. C#, C++ - double의 Infinity, NaN 표현 방식파일 다운로드1
11895정성태5/12/201913398.NET Framework: 832. ML.NET Model Builder - 회귀(Regression), 다중 분류(Multi-class classification) 예제파일 다운로드1
11894정성태5/10/201914559VS.NET IDE: 135. Visual Studio - ML.NET Model Builder 소개 [5]
11893정성태5/10/201912272오류 유형: 535. C# 6.0 이상의 문법을 컴파일 시 오류가 발생한다면?
11892정성태5/10/201912340웹: 38. HTTP Cookie의 expires 시간 형식(RFC7231)
11891정성태5/9/201914837.NET Framework: 831. (번역글) .NET Internals Cookbook Part 12 - Memory structure, attributes, handles
11890정성태5/8/201910552개발 환경 구성: 439. "Visual Studio Enterprise is required to execute the test." 메시지와 관련된 코드 기록
11889정성태5/8/201911316개발 환경 구성: 438. mstest, QTAgent의 로그 파일 설정 방법
11888정성태5/8/201926445.NET Framework: 830. C# - 비동기 호출을 취소하는 CancellationToken의 간단한 예제 코드 [1]파일 다운로드1
11887정성태5/8/201913119.NET Framework: 829. C# - yield 문을 사용할 수 있는 메서드의 조건
11886정성태5/7/201912160오류 유형: 534. mstest.exe 실행 시 "Visual Studio Enterprise is required to execute the test." 오류 [2]
11885정성태5/7/20199144오류 유형: 533. mstest.exe 실행 시 "File extension specified '.loadtest' is not a valid test extension." 오류 발생
11884정성태5/5/201912846.NET Framework: 828. C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 두 번째 이야기
11883정성태5/3/201917074.NET Framework: 827. C# - 인터넷 시간 서버로부터 받은 시간을 윈도우에 적용하는 방법파일 다운로드1
11882정성태5/2/201914039.NET Framework: 826. (번역글) .NET Internals Cookbook Part 11 - Various C# riddles파일 다운로드1
11881정성태4/28/201914318오류 유형: 532. .NET Core 프로젝트로 마이그레이션 시 "CS0579 Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute" 오류 발생
11880정성태4/25/201911087오류 유형: 531. 이벤트 로그 오류 - Task Scheduling Error: m->NextScheduledSPRetry 1547, m->NextScheduledEvent 1547
11879정성태4/24/201916342.NET Framework: 825. (번역글) .NET Internals Cookbook Part 10 - Threads, Tasks, asynchronous code and others파일 다운로드2
... 61  62  63  64  65  66  67  68  [69]  70  71  72  73  74  75  ...