通过C#在控制台输出各种图形文字

这不是要准备公司年会了嘛

每个部门抓壮丁,必须安排至少一个节目

想着上去唱首歌算了,被毙,没有部门特色

妈蛋,唱歌没特色,那隔壁在前线工作的部门要表演个啥,抄表?

冥思苦想之下,给节目加了点部门特色,在 Console 输出文字图形来报幕。

image

需求有了,现在就等实现。

不就是在 Console 输出点内容嘛,想当年在学校输出 9x9 乘法表的时候简直 6 的一批。

实操下来,妈蛋,没有规律,没有算法可言啊。

只能一点点去画?这要画到猴年马月。

继续冥思苦想,诶?识别图片每个像素点的颜色是不是可以?说干就干。

先准备一张白底黑字的图片

image

static void Main(string[] args)
{
	using (Bitmap bmp = new Bitmap("文件地址"))
	{
		for (int i = 0; i < bmp.Height; i++)
		{
			for (int j = 0; j < bmp.Width; j++)
			{
				Color pixelColor = bmp.GetPixel(j, i);
				if (IsBlack(pixelColor))
				{
					// 黑色
					Console.Write("*");
				}
				else
				{
					// 白色
					Console.Write(" ");
				}
			}
			Thread.Sleep(5);
			Console.Write("\n");
		}
	}
}

static bool IsBlack(Color color)
{
	return (color.R != 255 || color.G != 255 || color.B != 255);
}

代码就这么些代码,觉有有趣就分享出来一下。

再给大家分享一个可以修改 Console 字体大小的帮助类

public static class ConsoleHelper
{
	private const int FixedWidthTrueType = 54;
	private const int StandardOutputHandle = -11;

	[DllImport("kernel32.dll", SetLastError = true)]
	internal static extern IntPtr GetStdHandle(int nStdHandle);

	[return: MarshalAs(UnmanagedType.Bool)]
	[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
	internal static extern bool SetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool MaximumWindow, ref FontInfo ConsoleCurrentFontEx);

	[return: MarshalAs(UnmanagedType.Bool)]
	[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
	internal static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool MaximumWindow, ref FontInfo ConsoleCurrentFontEx);


	private static readonly IntPtr ConsoleOutputHandle = GetStdHandle(StandardOutputHandle);

	[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
	public struct FontInfo
	{
		internal int cbSize;
		internal int FontIndex;
		internal short FontWidth;
		public short FontSize;
		public int FontFamily;
		public int FontWeight;
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
		//[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.wc, SizeConst = 32)]
		public string FontName;
	}

	public static FontInfo[] SetCurrentFont(string font, short fontSize = 0)
	{
		FontInfo before = new FontInfo
		{
			cbSize = Marshal.SizeOf<FontInfo>()
		};

		if (GetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref before))
		{

			FontInfo set = new FontInfo
			{
				cbSize = Marshal.SizeOf<FontInfo>(),
				FontIndex = 0,
				FontFamily = FixedWidthTrueType,
				FontName = font,
				FontWeight = 400,
				FontSize = fontSize > 0 ? fontSize : before.FontSize
			};

			// Get some settings from current font.
			if (!SetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref set))
			{
				var ex = Marshal.GetLastWin32Error();
				throw new System.ComponentModel.Win32Exception(ex);
			}

			FontInfo after = new FontInfo
			{
				cbSize = Marshal.SizeOf<FontInfo>()
			};
			GetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref after);

			return new[] { before, set, after };
		}
		else
		{
			var er = Marshal.GetLastWin32Error();
			throw new System.ComponentModel.Win32Exception(er);
		}
	}
}
// 使用方式
ConsoleHelper.SetCurrentFont("Consolas", 50);

Console 的样式还是太单调了,老老实实剪视频去了。

posted @ 2022-02-16 10:04  畅饮无绪  阅读(1074)  评论(0编辑  收藏  举报