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();
            }
        }
    }
    

      

  • 相关阅读:
    bzoj1878: [SDOI2009]HH的项链
    bzoj1053: [HAOI2007]反素数ant
    bzoj2456: mode
    bzoj2330: [SCOI2011]糖果
    bzoj1050: [HAOI2006]旅行comf
    bzoj1047: [HAOI2007]理想的正方形
    bzoj1968: [Ahoi2005]COMMON 约数研究
    bzoj1046: [HAOI2007]上升序列
    bzoj2440: [中山市选2011]完全平方数
    bzoj1202: [HNOI2005]狡猾的商人
  • 原文地址:https://www.cnblogs.com/ZaraNet/p/9434014.html
Copyright © 2011-2022 走看看