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

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

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

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

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

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

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

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

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

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

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

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 的样式还是太单调了,老老实实剪视频去了。

通过C#在控制台输出各种图形文字的更多相关文章

  1. 【Python】在控制台输出不同颜色的文字

    今天调程序出了一个极为奇怪的问题,由于控制台输出挺多,就想把问题着重表示一下,具体即是在控制台输出红色文字. 于是在网上搜寻到了这篇:https://www.cnblogs.com/gongxr/p/ ...

  2. 利用ANSI转义序列在控制台输出彩色文字

    说明:无论什么语言,只要你的终端能够解释ANSI转义序列(大多数的类unix终端仿真器都能够解释ANSI转义序列,win32控制台则不支持),就能够使用ANSI转义序列输出颜色.这个功能看似鸡肋,但只 ...

  3. Java如何利用for循环在控制台输出正方形对角线图形

    1 /* 2 利用循环在控制台输出如下正方形对角线图形 3 * * * * * * * * * * * 4 * * * * 5 * * * * 6 * * * * 7 * * * * 8 * * * ...

  4. js控制台输出console

    介绍: js的console你可以在firefox的firedbug或者ie和google的f12调试模式下看到,这些主流浏览器的调试模式的控制可以输出一些信息,你的一些js代码测试可以直接在cons ...

  5. 前端不为人知的一面--前端冷知识集锦 前端已经被玩儿坏了!像console.log()可以向控制台输出图片

    前端已经被玩儿坏了!像console.log()可以向控制台输出图片等炫酷的玩意已经不是什么新闻了,像用||操作符给变量赋默认值也是人尽皆知的旧闻了,今天看到Quora上一个帖子,瞬间又GET了好多前 ...

  6. 自定义SpringBoot控制台输出的图案

    pringboot启动的时候,控制台输出的图案叫banner banner?啥玩意儿?相信有些人,一定是一脸懵逼... ——这个就不陌生了吧,这个是我们启动springboot的时候,控制台输出的.. ...

  7. python控制台输出颜色

    python_控制台输出带颜色的文字方法在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中.而一般的应用服务器,第三方库,甚至服务器的一些通告也 ...

  8. .Net Core 控制台输出中文乱码

    Net Core 控制台输出中文乱码的解决方法: public static void Main(string[] args)         {             Console.Output ...

  9. Canopy测试IPython控制台输出

    Canopy测试IPython控制台输出

随机推荐

  1. 基于ShardingJDBC的分库分表详细整理

    转载 https://www.cnblogs.com/jackion5/p/13658615.html 前言 传统应用项目设计通常都是采用单一数据库作为存储方案,但是随着互联网的迅猛发展以及应用数据量 ...

  2. Centos 7.6关闭selinux

    查看selinux状态 [root@localhost ~]# sestatus SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SE ...

  3. react中实现css动画

  4. gopher协议在SSRF漏洞中的作用

    1.什么是gopher协议?2.如何使用gopher协议反弹shell?3.在SSRF中如何使用gopher协议反弹shell? 一.什么是gopher协议?定义:Gopher是Internet上一个 ...

  5. echarts-gl初体验:使用echarts-gl实现3D地球

    首先我们要下载引入echarts.js和echarts-gl.js 有需要的自己拿资源哈 链接:https://pan.baidu.com/s/1J7U79ey-2ZN4pjb7RTarjg 提取码: ...

  6. 【刷题-LeetCode】215. Kth Largest Element in an Array

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

  7. 【刷题-LeetCode】154 Find Minimum in Rotated Sorted Array II

    Find Minimum in Rotated Sorted Array II Suppose an array sorted in ascending order is rotated at som ...

  8. 配置vscode的C++环境Unexpected GDB output from command "-environment-cd

    原因 中文字符 换成D盘目录下以后.

  9. unity3d录音

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class record : ...

  10. Qt之QColorDialog

    widget.h: #ifndef WIDGET_H #define WIDGET_H #include <QWidget> class Widget : public QWidget { ...