BitmapToASCii
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的更多相关文章
随机推荐
- UOJ#206. 【APIO2016】Gap 构造 交互题
原文链接www.cnblogs.com/zhouzhendong/p/UOJ206.html 题解 T = 1 的情况直接大力从两边向中间询问即可. T = 2 的情况挺妙的,我没想到. 考虑首先花费 ...
- Spring中@Autowired和@Resource两种自动装配的方法
@Autowired 默认按bean类型查找并注入,若此时有多个相同类型的bean时,按bean name查找则为:@Autowired @Qulifer(value=”bean名称”). @Reso ...
- Linux磁盘和文件系统管理
1.检测并确认新硬盘 挂载好新的硬盘设备并启动主机后,Linux系统会自动检测并加载该硬盘,无须额外安装驱动.执行“fdisk -l”命令即可查看,确认新增硬盘的设备名称和位置.作用:列出当前系统中所 ...
- PBRT笔记(8)——材质
BSDF类 表面着色器会绑定场景中每一个图元(被赋予了这个着色器),而表面着色器则由Material类的实例来表示.它会拥有一个BSDF类对象(可能是BSSDF),用于计算表面上每一点的辐射度(颜色) ...
- 传入两坐标点,利用div+css画线
上样式生成函数代码 lineStyle (x1, y1, x2, y2, lineWidth = 4, color = 'black') { let rectX = x1 < x2 ? x1 : ...
- c# winform打印excel(使用NPOI+Spire.xls+PrintDocument直接打印excel)
前言 c#做winform程序要求生成并打印Excel报告,为了不安装Office相应组件,我选择了NPOI来生成Excel报告,用winform的PrintDocument控件来触发打印操作,而难点 ...
- 180815 Python自学成才001
1.为什么学习Python? Python:脚本语言,易入门,可移植. Python适用范围:web开发.自动化测试工具编写. 适用岗位:运维开发(运维).自动化测试(软件测试).Python开发(软 ...
- 修改 Docker 的 daemon.json后启动失败
创建Harbor要把register 换成Harbor地址 vim /etc/docker/daemon.json添加{ "insecure-registries":[" ...
- LeetCode 单链表专题 (一)
目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...
- 记一次JVM故障排除
今天,自己开发的事件驱动的java大规模爬虫程序上线了几个新任务后突然异常. 异常: 程序业务异常,经查看CPU利用率满,内存满,一直报OOM,目测有内存泄露.如下图所示,四核16G的内粗,CPU高达 ...