文字像素(.NET)

无图言*

代码实现

新建一个控制台应用程序, 调整 Program.cs 文件内容如下:

using System;
using System.Drawing;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "文字像素(.NET)";
            Console.WriteLine(value: "请输入任何文本,最好不要太长!");
            string str = Console.ReadLine();

            using (Bitmap bitmap = new Bitmap(width: 100, height: 12))
            {
                using (Graphics graphics = Graphics.FromImage(image: bitmap))
                {
                    graphics.Clear(color: Color.White);
                    graphics.DrawString(s: str, font: new Font(familyName: "宋体", emSize: 10), brush: Brushes.Black, x: 0, y: 0);
                    graphics.FillEllipse(brush: Brushes.White, x: 10, y: 10, width: 10, height: 10);
                }

                for (int y = 0; y < bitmap.Height; y++)
                {
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        Console.Write(value: bitmap.GetPixel(x: x, y: y).ToArgb() == Color.White.ToArgb() ? " " : "*");
                    }
                    Console.WriteLine();
                }
            }
            Console.ReadKey();
        }
    }
}

注意事项

.NET Framework 中内置了 System.Drawing 库, 但是在 .NET Core 中因为某些原因没有内置, 需要通过 NuGet 下载 System.Drawing.Common

posted @ 2020-01-18 16:35  taadis  阅读(228)  评论(0编辑  收藏  举报