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. this指向及改变this指向的方法

    一.函数的调用方式决定了 this 的指向不同,但总的原则,this指的是调用函数的那个对象: 1.普通函数调用,此时 this 指向 全局对象window function fn() { conso ...

  2. unity一个按钮实现开和关

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class ButtonCl ...

  3. Quartz+JAVA+Servlet实现任务调度系统(简洁)

    前言 该系统使用场景: 在12306上买了一张火车票,30分钟内需要支付(需要添加一个倒计时),30分钟还没有支付就请求取消订单的接口(自动根据url请求),如果支付了收到了支付的回调通知后,就删除计 ...

  4. session的几种模式

    https://www.cnblogs.com/panchunting/archive/2012/06/25/ASPNET_Session_Mode.html token的生成和应用 session和 ...

  5. Oracle:select into 查询没有记录的解决办法

    在数据库编程中,select into 语句可以将数据库的某些值赋值给程序的变量,使用起来非常方便.但很多时候也会遇到查询出来没有记录的情况,这时程序会出错. 可以使用 exception when ...

  6. scrapy + selenium 的动态爬虫

    动态爬虫 在通过scrapy框架进行某些网站数据爬取的时候,往往会碰到页面动态数据加载的情况发生,如果直接使用scrapy对其url发请求,是绝对获取不到那部分动态加载出来的数据值.但是通过观察我们会 ...

  7. CentOS下SVN服务的启动与关闭

    CentOS下SVN服务的启动与关闭 操作系统:CentOS 6.5  SVN版本:1.8.11 启动SVN服务:  svnserve -d -r /home/svn /home/svn 为版本库的根 ...

  8. tf.contrib.slim.data数据加载 综述

    TF-Slim为了方便加载各种数据类型(如TFRocords或者文本文件)的数据,创建了这个库. Dataset 这里的数据库与通常意义下数据库是不同的,这里数据库是python一个类,它负责将原始数 ...

  9. PHP 清除 Excel 导入的数据空格

    处理excel中的数据时,遇到了页面中显示为空格,审查元素时却显示为换行,使用replace函数也不管用,反正就是不知道是什么东西,看起来像空格 中文空格这里面有好几种:没有简单的解决问题的方式,比如 ...

  10. Git生成ssh密钥指定文件

    ssh-keygen 使用的时候可以直接使用 -f 参数 指定密钥保存文件,省去后面生成成功后再提示选择保存文件: ssh-keygen -t rsa -C "abc@example.com ...