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
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] 픽셀 스냅(Pixel Snap) 사용하기 (0) | 2014.02.21 |
---|---|
[C#/WPF] GridViewColumn 클래스 : 바인딩 설정하기 (0) | 2014.02.21 |
[C#/WPF] Window 클래스 : 윈도우 객체 핸들 구하기 (0) | 2014.02.21 |
[C#/WPF] Window 클래스 : 시스템 메뉴 숨기기 (0) | 2014.02.21 |
[C#/WPF] Dispatcher 클래스 : Invoke 메소드를 사용해 크로스 스레드(Cross Thread) 처리하기 (0) | 2014.02.21 |
[C#/WPF] SolidColorBrush 클래스 : 디폴트 시스템 색상 오버라이딩 하기 (0) | 2014.02.20 |
[C#/WPF] Ellipse 엘리먼트 : 커졌다 사라지는 동심원 애니메이션 설정하기 (0) | 2014.02.20 |
[C#/WPF] BitmapSource 클래스 : WINFORM Icon 객체에서 비트맵 소스 구하기 (0) | 2014.02.20 |
[C#/WPF] BitmapSource 클래스 : WINFORM Bitmap 객체에서 비트맵 소스 구하기 (0) | 2014.02.20 |
[C#/WPF] ListBox 엘리먼트 : ItemContainerStyle 속성을 사용해 선택한 Icon 항목을 크게 만드는 애니메이션 설정하기 (0) | 2014.02.20 |