做的一个ascii_art demo

思路很简单,将每个像素点取得的值,映射到ascii 表中,将取得的字符追加到字符串中,最终输出。

缺点是处理大图片时,会生成非常大的字符表,简单的用抽值和压缩图像弥补,但效果还是不容乐观,有更好的方法,欢迎沟通,互促互利!

 

捕获

主要代码如下:   
 #region 定义成员变量
        private string file_name = "";
        private   string [] ascii_map = {"#", "b"," ", "d", "e","f"," ","*","-","k",".","l","m","n","o",
                                                      "p","q","r","s","t","u","v","w","x","y","z",
                                                      "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O",
                                                      "P","Q","R","S","T","U","V","W","X","Y","Z",
                                                      "1","2","3","4","g","6","7","8","9","0", "5",
                                                      ",","j","/","<",">","?","`","!","@","a","$","%","^","&","h","(",")","_","+",
                                                      "i","=","[","]"
                                                        };
        private img_compress img_handle = new img_compress();
        #endregion
        #region  图片压缩
        private Bitmap img_compress(Bitmap pic)
        {
            byte[] buffer = img_handle.ImageGdi(pic);
            pic =  img_handle.bytetobmp(buffer);
            return pic;
        }
        #endregion
        #region 按钮事件
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog image_file = new OpenFileDialog();          
            if (image_file.ShowDialog() == DialogResult.OK)
            {
                file_name = image_file.FileName;
                pictureBox1.Image = Image.FromFile(file_name);
            }        
        }

        private void button2_Click(object sender, EventArgs e)
        {   
            Color grey_color;
            int average_color;
            StringBuilder pic_text = new StringBuilder();
            Bitmap pic = new Bitmap(file_name);
            
            pic = img_compress(pic);
            for (int i = 0; i < pic.Height; i = i + 3)
            {
                for (int j = 0; j < pic.Width; j = j + 2)
                {
                    Color pixel = pic.GetPixel(j, i);
                    average_color = (pixel.R + pixel.G + pixel.B) / 3;
                    grey_color = Color.FromArgb(average_color, average_color, average_color);
                    pic_text.Append(ascii_map[(grey_color.R * 10) /255]);
                }
                pic_text.Append("\n");               
            }
            richTextBox1.Text = pic_text.ToString();     
        }
        #endregion
    }

//IMG_COMPRESS.cs
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace IMG_COMPRESS
{
    class img_compress
    {
        public img_compress()
        {
            ;
        }

        #region Bitmap转换byte[]数组
        public byte[] Bmptobyte(Bitmap bmp)
        {
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, ImageFormat.Jpeg);
            ms.Flush();
            byte[] buffer = ms.GetBuffer();
            ms.Close();
            return buffer;
        }
        #endregion

        #region byte[]数组转换Bitmap
        public Bitmap bytetobmp(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream();
            ms.Write(buffer, 0, buffer.Length);
            Bitmap bmp = new Bitmap(ms);
            ms.Close();
            return bmp;
        }
    #endregion
  
        #region GDI压缩图片     
        public byte[] ImageGdi(Bitmap bmp)
        {
            Bitmap xbmp = new Bitmap(bmp);
            MemoryStream ms = new MemoryStream();
            xbmp.Save(ms, ImageFormat.Jpeg);
            byte[] buffer;
            ms.Flush();
            if (ms.Length > 95000)
            {
                buffer = ms.GetBuffer();
                double new_width = 0;
                double new_height = 0;

                Image m_src_image = Image.FromStream(ms);
                if (m_src_image.Width >= m_src_image.Height)
                {
                    new_width = 1024;
                    new_height = new_width * m_src_image.Height / (double)m_src_image.Width;
                }
                else if (m_src_image.Height >= m_src_image.Width)
                {
                    new_height = 768;
                    new_width = new_height * m_src_image.Width / (double)m_src_image.Height;
                }

                Bitmap bbmp = new Bitmap((int)new_width, (int)new_height, m_src_image.PixelFormat);
                Graphics m_graphics = Graphics.FromImage(bbmp);
                m_graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                m_graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                m_graphics.DrawImage(m_src_image, 0, 0, bbmp.Width, bbmp.Height);

                ms = new MemoryStream();

                bbmp.Save(ms, ImageFormat.Jpeg);
                buffer = ms.GetBuffer();
                ms.Close();

                return buffer;
            }
            else
            {
                buffer = ms.GetBuffer();
                ms.Close();
                return buffer;
            }
        }
        #endregion
    }
}

转载于:https://www.cnblogs.com/seebro/archive/2012/06/03/2533098.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值