- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
[C#] Hướng dẫn chuyển đổi hình ảnh sang image Art (ASCII)
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#:
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!