Microsoft MVP성태의 닷넷 이야기
.NET Framework: 498. C#으로 간단하게 만들어 본 ASCII Art 프로그램 [링크 복사], [링크+제목 복사],
조회: 17031
글쓴 사람
정성태 (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 인코딩에 해당합니다.
정성태

... 91  92  93  94  95  96  97  98  99  100  101  102  103  [104]  105  ...
NoWriterDateCnt.TitleFile(s)
11040정성태9/10/201618984.NET Framework: 604. C# Windows Forms - Drag & Drop 예제 코드 [2]파일 다운로드1
11039정성태9/9/201616200오류 유형: 355. Visual Studio 빌드 오류 - error CS0122: '__ComObject' is inaccessible due to its protection level
11038정성태9/9/201617276VC++: 99. 서로 다른 프로세스에서 WM_DROPFILES 메시지를 전송하는 방법파일 다운로드1
11037정성태9/8/201620509.NET Framework: 603. socket - shutdown 호출이 필요한 사례파일 다운로드1
11036정성태8/29/201617516개발 환경 구성: 297. 소스 코드가 없는 닷넷 어셈블리를 디버깅할 때 지역 변숫값을 확인하는 방법
11035정성태8/29/201613551오류 유형: 354. .NET Reflector - PDB 생성 화면에서 "Clear Store"를 하면 "Index and length must refer to a location within the string" 예외 발생
11034정성태8/25/201617242개발 환경 구성: 296. .NET Core 프로젝트를 NuGet Gallery에 배포하는 방법 [2]
11033정성태8/24/201615064오류 유형: 353. coreclr 빌드 시 error C3249: illegal statement or sub-expression for 'constexpr' function
11032정성태8/23/201614503개발 환경 구성: 295. 최신의 Visual C++ 컴파일러 도구를 사용하는 방법 [1]
11031정성태8/23/201611588오류 유형: 352. Error encountered while pushing to the remote repository: Response status code does not indicate success: 403 (Forbidden).
11030정성태8/23/201613360VS.NET IDE: 111. Team Explorer - 추가한 Git Remote 저장소가 Branch에 보이지 않는 경우
11029정성태8/18/201619835.NET Framework: 602. Process.Start의 cmd.exe에서 stdin만 redirect 하는 방법 [1]파일 다운로드1
11028정성태8/15/201614835오류 유형: 351. Octave 설치 시 JRE 경로 문제
11027정성태8/15/201615698.NET Framework: 601. ElementHost 컨트롤의 메모리 누수 현상
11026정성태8/13/201616344Math: 19. 행렬 연산으로 본 해밍코드
11025정성태8/12/201614886개발 환경 구성: 294. .NET Core 프로젝트에서 "Copy to Output Directory" 처리 [1]
11024정성태8/12/201615278오류 유형: 350. "nProtect GameMon" 실행 중에는 Visual Studio 디버깅이 안됩니다! [1]
11023정성태8/10/201616107개발 환경 구성: 293. Azure 구독 후 PaaS 서비스 만들어 보기
11022정성태8/10/201616469개발 환경 구성: 292. Azure Cloud Service 배포시 사용자 정의 작업을 추가하는 방법
11021정성태8/10/201614161오류 유형: 349. System.Runtime.Remoting.RemotingException - Type '..., ..., Version=..., Culture=neutral, PublicKeyToken=null' is not registered for activation [2]
11020정성태8/10/201615977VC++: 98. 원본과 대상 버퍼가 같은 경우 memcpy, wmemcpy 주의점
11019정성태8/10/201630898기타: 60. 도서: 시작하세요! C# 6.0 프로그래밍: 기본 문법부터 실전 예제까지 (2쇄 정오표)
11018정성태8/9/201616674.NET Framework: 600. 단일 메서드 내에서의 할당으로 알아보는 자바와 닷넷의 GC 차이점 [1]
11017정성태8/9/201619781웹: 33. HTTP 쿠키에 한글 값을 설정하는 방법
11016정성태8/7/201616700개발 환경 구성: 291. Windows Server Containers 소개
11015정성태8/7/201615073오류 유형: 348. Windows Server 2016 TP5에서 Windows Containers의 docker run 실행 시 encountered an error during Start failed in Win32
... 91  92  93  94  95  96  97  98  99  100  101  102  103  [104]  105  ...