NEWS

[C#] Hướng dẫn chuyển đổi hình ảnh sang image Art (ASCII)

[C#] Hướng dẫn chuyển đổi hình ảnh sang image Art (ASCII)
Đăng bởi: Thảo Meo - Lượt xem: 6627 15:38:55, 23/07/2019C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục chia sẽ đến các bạn source code dùng để chuyển đổi hình ảnh sang hình ảnh sử dụng mã ASCII hay còn gọi là Image Art trong C#.

[C#] Chuyển hình ảnh sang Image Art ACSII

Vậy Image Art là gì?

Những kí tự biểu cảm tin nhắn thường dùng cũng là một bộ môn nghệ thuật Σ( ° △ °|||) Bạn đã biết hay chưa?

Bạn cực vui (^_^) hoặc là đang rất buồn (T^T). Bạn nhận được tin nhắn chúc mừng năm mới HAPPY NEW YEAR ツ và cả chúc mừng sinh nhật ♫♪♥♥[H][A][P][P][Y] [B][I][R][T][H][D][A][Y]♥♥♫♪. Bạn vẫn thường sử dụng chúng hằng ngày cuối mỗi tin nhắn hoặc status của bạn. Tất nhiên, những người xung quanh bạn cũng vậy. 

Dưới đây là giao diện demo chuyển hình ảnh sang image ascii C#:

image_art_csharp

Source code Image ART C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ASCII_Art
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string filename;

        private void btn_browser_Click(object sender, EventArgs e)
        {
            using (var dlg = new OpenFileDialog())
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    filename = dlg.FileName;
                    pictureBox1.Image = Image.FromFile(filename);
                }
            }
        }

        private void btn_convert_Click(object sender, EventArgs e)
        {
            Image img = Image.FromFile(filename);
            Bitmap bmp = new Bitmap(img, 100, 100);
            show_image(convert_image(bmp));
        }

        private unsafe StringBuilder convert_image(Bitmap bmp)
        {
            StringBuilder asciiResult = new StringBuilder();
            asciiResult.Append("");

            int bmpHeight = bmp.Height;
            int bmpWidth = bmp.Width;

            BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmpWidth, bmpHeight), ImageLockMode.ReadOnly, bmp.PixelFormat);

            int bmpStride = bmpData.Stride;
            byte* currentPixel = (byte*)bmpData.Scan0;

            for (int y = 0; y < bmpHeight; y++)
            {
                for (int x = 0; x < bmpWidth; x++)
                {
                    int r = currentPixel[x * 4];
                    int g = currentPixel[x * 4 + 1];
                    int b = currentPixel[x * 4 + 2];
                    int alpha = currentPixel[x * 4 + 3];
                     asciiResult.Append($@"<span style='color: rgb({r},{ g},{ b}); '>{getAsciiChar(alpha)}</span>");

                }
                currentPixel += bmpStride;
                asciiResult.Append("<br>");

            }
            asciiResult.Append("<br>");

            bmp.UnlockBits(bmpData);
            return asciiResult;
        }

        private char getAsciiChar(int alpha)
        {
            if (alpha >= 240)
                return '@';
            if (alpha >= 200)
                return '#';
            if (alpha >= 160)
                return '$';
            if (alpha >= 120)
                return '%';
            if (alpha >= 80)
                return '8';
            if (alpha >= 40)
                return '|';

            return '.';
        }
        private void show_image(StringBuilder asciiResult)
        {
            StreamWriter sw = new StreamWriter("image.html");
            sw.Write(asciiResult.ToString());
            sw.Close();

            string html = asciiResult.ToString();
            webBrowser1.DocumentText = html;
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowser1.Document.Body.Style = "zoom:20%;";
        }
    }
}

Chúc các bạn có thời gian thư giản và vui vẻ với ứng dụng này :)

Thanks for watching!

 

DOWNLOAD SOURCE

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn chuyển đổi hình ảnh sang image Art (ASCII)
Đăng bởi: Thảo Meo - Lượt xem: 6627 15:38:55, 23/07/2019C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.