using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace PicToASCii
{
public static class ASCiiHelper
{
public static string Generate(Bitmap bitmap, int rowSize, int colSize, int type)
{
StringBuilder result = new StringBuilder();
char[] charset = { ' ', '.', ',', ':', ';', 'i', '1', 'r', 's', '5', '3', 'A', 'H', '9', '8', '&', '@', '#' };
if (type == 1)
{
//charset = new char[] { '8', '9', '6', '4', '3', '5', '7', '0', '2', '1', '.',' ' };
charset = new char[] { ' ', '.', '1', '2', '0', '7', '5', '3', '4', '6', '9', '8' };
}
else if (type == 2)
{
charset = new char[] { '丶', '卜', '乙', '日', '瓦', '車', '馬', '龠', '齱', '龖' };
}
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;
float averBright = 0;
for (int j = 0; j < rowSize; j++)
{
for (int i = 0; i < colSize; i++)
{
try
{
Color color = bitmap.GetPixel(offsetX + i, offsetY + j);
averBright += color.GetBrightness();
}
catch (ArgumentOutOfRangeException)
{
averBright += 0;
}
}
}
averBright /= (rowSize * colSize);
int index = (int)(averBright * charset.Length);
if (index == charset.Length)
index--;
result.Append(charset[charset.Length - 1 - index]);
}
result.Append("\r\n");
}
return result.ToString();
}
}
}

  

BitmapToASCii的更多相关文章

随机推荐

  1. Python解析XML文件

    XML与JSON的互相转化详见:XML模块 https://www.cnblogs.com/shengyang17/p/8606223.html <?xml version="1.0& ...

  2. idea看源码

    idea看源码,可以直接搜索.看接口具体调用的是哪个类里面的方法(多态)

  3. idea+scala sdk + scala插件

    0X01 前言 我的主语言是python,说起java,想起了大二(三年前)上课时教过,课程设计的时候曾经做过个俄罗斯方块,后面其他设计copy代码读懂代码(再后面的课设就用python了). 本次涉 ...

  4. JAVA基础复习与总结<九> 线程的基本概念_Thread继承创建线程

    多线程 一.线程的概念 1.1 程序.进程.线程 程序:Program 是一个静态的概念 进程:Process 是一个动态的概念 进程是程序的一次动态执行过程,占用特定的地址空间. 每个进程都是独立的 ...

  5. hdu1814 Peaceful Commission

    hdu1814 Peaceful Commission 题意:2-sat裸题,打印字典序最小的 我写了三个 染色做法,正解 scc做法,不管字典序 scc做法,错误的字典序贪心 #include &l ...

  6. html5 input输入实时检测以及延时优化

    有个项目是,这么个情况,输入框,实时监测输入,触发请求. 第一想法是input 上的onchange()方法,试了一下,不好用,是值等更改确认了,才会触发,不即时. 上网查了一下, $("# ...

  7. 近期待学习&目标内容

    算法 Splay 树链剖分 AC自动机 问题 bzoj1010[HNOI2008]玩具装箱 bzoj1096[ZJOI2007]仓库建设 bzoj1597[USACP2008 Mar]土地购买 bzo ...

  8. List使用linq的OrderBy方法排序,并按照两个字段排序的写法

    SfaMember.GetList(searchInfo, 0, 1000, out Allcount).Where(item => item.bOpen == true).OrderBy(it ...

  9. c++实验二

    1.函数重载编程练习编写重载函数add(),实现对int型,double型,Complex型数据的加法 #include<iostream> using namespace std; st ...

  10. python错误和异常

    一:语法错误syntax errors        熟悉语法! 二:异常 ①打印错误信息时,异常的类型作为异常的内置名显示,并以调用栈的形式显示具体信息    ②常见的异常:             ...