using System.Collections.Generic;
using System.IO.Compression;
namespace ConsoleApplication
public static byte[] StringToByteArray(string hex) {
hex=new String(hex.Where(x => x>='0' && x<='F').ToArray());
return Enumerable.Range(0, hex.Length)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
static string Decompress(byte[] gzBuffer)
using (MemoryStream ms = new MemoryStream())
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 0, gzBuffer.Length - 0);
byte[] buffer = new byte[msgLength];
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
zip.Read(buffer, 0, buffer.Length);
return Encoding.UTF8.GetString(buffer).Substring(0,999);
public static String BitmapConvert(Bitmap bitmap, int rowSize, int colSize)
StringBuilder result = new StringBuilder();
var charset="$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ".ToCharArray().Reverse().ToArray();
int bitmapH = bitmap.Height;
int bitmapW = bitmap.Width;
for (int h = 0; h < bitmapH / rowSize; h++)
int offsetY = h * rowSize;
for (int w = 0; w < bitmapW / colSize; w++)
int offsetX = w * colSize;
for (int j = 0; j < rowSize; j++)
for (int i = 0; i < colSize; i++)
Color color = bitmap.GetPixel(offsetX + i, offsetY + j);
averBright += color.GetBrightness();
catch (ArgumentOutOfRangeException)
averBright /= (rowSize * colSize);
int index = (int)(averBright * charset.Length);
if (index == charset.Length)
result.Append(charset[charset.Length - 1 - index]);
return result.ToString();
public static void Main(string[] args)
Console.WriteLine(BitmapConvert(new Bitmap(Bitmap.FromStream(new System.Net.WebClient().OpenRead("http://p21cajw5z.bkt.clouddn.com/app1/images/%E7%81%B0%E5%8E%9F%E5%93%80.jpg"))),