■ ASCII ART 문자열을 구하는 방법을 보여준다.
▶ ASCIIARTElement.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
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 } |
▶ CreateASCIIARTElementList 메소드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
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 |
▶ GetASCIIARTString 메소드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
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 |