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