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的更多相关文章
随机推荐
- 广搜迷之RE及迷之WA
最近做广搜的时候天天迷之RE,经过dalao@口昭寿指点,我把string数组换成了char二维数组就AC了,(然而我并不知道为什么) 传送门 <——以这个题为例 #include <b ...
- HTML学习笔记【思维导图版】
- mysql查询出近一周,三个月,一年的数据
SELECT * FROM 表名 WHERE 时间字段>DATE_SUB(CURDATE(), INTERVAL YEAR) 一年 SELECT * FROM 表名 WHERE 时间字段> ...
- Python第三周第一次作业中关于工程目录各种导入的模拟学习
目录 Python工程目录 导入自定义模块, 包 记录的缘由 模块搜索路径 模块: 导入模块 导入函数 导入类 多个类 @(Python第三周第一次作业中工程目录,模拟学习) Python工程目录 导 ...
- git diff old mode 100644 new mode 100755
今天执行git diff filename ,出现 old mode 100644 new mode 100755 的提示,如下图: 但是发现文件内容并没有发生改变 想起来中间执行过chmod 的操 ...
- input里面的submit鼠标按钮属性cursor
属性cursor 属性值: pointer 小手 move 移动 help 帮助 wait 等待
- Cocos2d-js和Android交互
说白了,就是JavaScript和Java之间的函数互相调用. 先看一下效果 有了这个交互,为了以后接sdk做准备. 要点: javascript调用java: jsb.reflection.call ...
- vbs脚本实现qq定时发消息(初级)
vbs脚本实现QQ消息定时发送 目标 批处理又称为批处理脚本,强大的强大功能可以高效得实现很多功能,例如批量更改文件格式,批量进行文件读写,今天我们的目标是用vbs脚本编写可以发送qq消息的脚本,并利 ...
- HTML/CSS实现的一个列表页
又到休息日,白天没事跟朋友去逛逛街,侃大山,晚上了,上网无趣,于是就想起该练练了, 这次是做了一个页面,最上面是一个banner 用到了一个jQuery的逻辑判断当banner初始top值小于wind ...
- iOS 开发中keyChain的使用
我们开发中很多数据都是直接存储到本地沙盒中的,这样当应用程序被卸载后,本地的数据都会被删除.如果我们不想让数据在卸载程序的时候丢失,我们可以用KeyChain来存储我们想要的数据.苹果提供了原生的一套 ...