通過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 的樣式還是太單調了,老老實實剪視頻去了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM