zoukankan      html  css  js  c++  java
  • 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("
    ");
                }
                return result.ToString();
            }
        }
    }
    

      

  • 相关阅读:
    C#特性
    C#继承
    对FileStream的几种属性和方法认识
    C# Parallel用法
    c#发布补丁
    WebApi接收接收日期格式参数时,日期类型(2019-10-08T16:00:00.000Z)后台接收时间少8小时问题
    c# 基于WebApi的快速开发框架FastFramework
    c# webapi结合swagger的使用
    windows服务autofac注入quartz任务
    c# autofac结合WebApi的使用
  • 原文地址:https://www.cnblogs.com/ZaraNet/p/9434014.html
Copyright © 2011-2022 走看看