首页 >电脑软件 > C#将图片转换为ASCii字符 V1.0 绿色免费版
C#将图片转换为ASCii字符 V1.0 绿色免费版

C#将图片转换为ASCii字符 V1.0 绿色免费版

软件大小:50KB

软件语言:简体中文

软件类别:应用工具

更新时间:2026-02-02 13:06:16

版本:v1.0 绿色免费版

应用平台:Windows平台

  • 软件介绍
  • 软件截图
  • 相关软件
  • 相关阅读
  • 下载地址

C#将图片转换为ASCii字符 V1.0绿色免费版是一款实用的小工具,能快速把图片转化为ASCii字符,且完全免费、绿色无插件。我超喜欢它的“快速转换”功能。之前我想把一张图片做成字符画用于创意展示,用其他软件操作复杂还容易出错。而用这款工具,只需上传图片,瞬间就能完成转换,大大节省了时间和精力,轻松满足了我的需求。

使用说明

长宽是设定 要替换成字符的像素块大小 越小 越精细。
输出的是文本文件,保存路径为D:\test.txt,开txt文件 如太大 把 txt字体设置成最小 就可以看到效果了。

代码说明

private void button1_Click(object sender, EventArgs e)

{

string str = null;

//上传照片

OpenFileDialog op = new OpenFileDialog();

if (op.ShowDialog() == DialogResult.OK)

{

var image = File.ReadAllBytes(op.FileName);

MemoryStream ms1 = new MemoryStream(image);

Bitmap bm = (Bitmap)Image.FromStream(ms1);

str = Generate.GenerateStr(bm, int.Parse(comboBox2.Text),

int.Parse(comboBox1.Text), int.Parse(comboBox3.Text));

}

this.btnSave_Click(str);

textBox1.Text = "已生成,文件位置D:\\test.txt";

}

private void btnSave_Click(string s)

{

StreamWriter sw = File.AppendText(@"D:\\test.txt"); //保存到指定路径

sw.Write(s);

sw.Flush();

sw.Close();

}

/// <summary>

/// 生成string

/// </summary>

/// <param name="bitmap">照片</param>

/// <param name="rowSize">行大小</param>

/// <param name="colSize">列大小</param>

/// <param name="type">模式</param>

/// <returns></returns>

public static string GenerateStr(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[] { ' ', '.', '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 + 1, 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("\r\n");

}

return result.ToString(); }

软件截图
  • C#将图片转换为ASCii字符 V1.0 绿色免费版
  • C#将图片转换为ASCii字符 V1.0 绿色免费版下载地址

相关软件

相关阅读