첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

▶ ASCIIARTElement.cs

using System.Windows.Media;

/// <summary>
/// ASCII ART 요소
/// </summary>
public struct ASCIIARTElement 
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 문자열 - String

    /// <summary>
    /// 문자열
    /// </summary>
    public string String
    {
        get;
        set;
    }

    #endregion

    #region 색상 - Color

    /// <summary>
    /// 색상
    /// </summary>
    public Color Color
    {
        get;
        set;
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region HTML 문자열 구하기 - GetHTMLString(fontSize)

    /// <summary>
    /// HTML 문자열 구하기
    /// </summary>
    /// <param name="fontSize">폰트 크기</param>
    /// <returns>HTML 문자열</returns>
    public string GetHTMLString(int fontSize)
    {
        string colorString = string.Format("#{0}", Color.ToString().Substring(3));

        return string.Format
        (
            "<font color=\"{0}\" style=\"font-size:{1}px\">{2}</font>",
            colorString,
            fontSize,
            String
        );
    }

    #endregion
}

 

728x90

 

▶ CreateASCIIARTElementList 메소드

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

#region ASCII ART 엘리먼트 리스트 구하기 - CreateASCIIARTElementList(filePath, imageWidth, imageHeight)

/// <summary>
/// ASCII ART 엘리먼트 리스트 구하기
/// </summary>
/// <param name="filePath">파일 경로</param>
/// <param name="imageWidth">이미지 너비</param>
/// <param name="imageHeight">이미지 높이</param>
/// <returns>ASCII ART 엘리먼트 리스트</returns>
public List<ASCIIARTElement> CreateASCIIARTElementList(string filePath, int imageWidth, int imageHeight)
{
    List<ASCIIARTElement> list = new List<ASCIIARTElement>();

    int bitmapHeaderSize = 54;

    BitmapImage bitmapImage = new BitmapImage(new Uri(filePath));

    BmpBitmapEncoder bmpBitmapEncoder = new BmpBitmapEncoder();

    bmpBitmapEncoder.Frames.Add(BitmapFrame.Create(bitmapImage));

    MemoryStream memoryStream = new MemoryStream();

    bmpBitmapEncoder.Save(memoryStream);

    memoryStream.Seek(bitmapHeaderSize, SeekOrigin.Begin);

    int spaceX = bitmapImage.PixelWidth  > imageWidth  ? bitmapImage.PixelWidth  / imageWidth  : 1;
    int spaceY = bitmapImage.PixelHeight > imageHeight ? bitmapImage.PixelHeight / imageHeight : 1;
    int offset = 0;

    Random random = new Random((int)DateTime.Now.TimeOfDay.TotalMilliseconds);

    for(int y = bitmapImage.PixelHeight - 1; y > 0; y -= spaceY)
    {
        for(int x = 0; x < bitmapImage.PixelWidth; x += spaceX)
        {
            offset = (bitmapImage.PixelWidth * 4) * y + (x * 4) + bitmapHeaderSize;

            memoryStream.Seek(offset, SeekOrigin.Begin);

            Color color = new Color();

            color.A = 255;
            color.B = (byte)memoryStream.ReadByte();
            color.G = (byte)memoryStream.ReadByte();
            color.R = (byte)memoryStream.ReadByte();

            ASCIIARTElement element = new ASCIIARTElement();

            element.Color  = color;
            element.String = ((char)(random.Next('A', 'Z'))).ToString();

            list.Add(element);
        }

        list.Add(new ASCIIARTElement() { String = "<br>" });
    }

    return list;   
}

#endregion

 

300x250

 

▶ GetASCIIARTString 메소드

using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Media;

#region ASCII ART 문자열 구하기 - GetASCIIARTString(filePath, imageWidth, imageHeight, fontFamily, fontSize)

/// <summary>
/// ASCII ART 문자열 구하기
/// </summary>
/// <param name="filePath">파일 경로</param>
/// <param name="imageWidth">이미지 너비</param>
/// <param name="imageHeight">이미지 높이</param>
/// <param name="fontFamily">폰트 패밀리</param>
/// <param name="fontSize">폰트 크기</param>
/// <returns>ASCII ART 문자열</returns>
public string GetASCIIARTString(string filePath, int imageWidth, int imageHeight, FontFamily fontFamily, int fontSize)
{
    List<ASCIIARTElement> list = CreateASCIIARTElementList(filePath, imageWidth, imageHeight);

    StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.AppendFormat("<body bgcolor=\"black\"><center><font face=\"{0}\">", fontFamily);

    foreach(ASCIIARTElement element in list)
    {
        stringBuilder.Append(element.GetHTMLString(fontSize));
    }

    stringBuilder.Append("</font></center></body>");

    return stringBuilder.ToString();
}

#endregion

 

728x90
그리드형(광고전용)
Posted by icodebroker
,