通过C#在控制台输出各种图形文字
2024-08-23 15:07:12
这不是要准备公司年会了嘛
每个部门抓壮丁,必须安排至少一个节目
想着上去唱首歌算了,被毙,没有部门特色
妈蛋,唱歌没特色,那隔壁在前线工作的部门要表演个啥,抄表?
冥思苦想之下,给节目加了点部门特色,在 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
的样式还是太单调了,老老实实剪视频去了。
最新文章
- mybatis Oracle 批量插入,批量更新
- 浅谈WEB前后端分离
- AppDelegate动态加载StoryBoard
- ASP.NET4.5Web API及非同步程序开发系列(2)
- 生活life
- Monte Carlo方法简介(转载)
- STL Map的使用
- javaweb学习总结(二十四)——jsp传统标签开发
- [Hibernate] - Criteria Select
- vue通过判断写样式(v-bind)
- 08_rlCoachKin自主编译,调试
- Xcode断点的一些黑魔法
- Google推出iOS功能性UI测试框架EarlGrey
- nginx的配置服务器集群,负载均衡
- Java基础(4)-数组
- Andrew Ng机器学习课程笔记--汇总
- JAVA 的 Date、Calendar的常用用法
- 解决Eclipse中无法直接使用Base64Encoder的问题(转载)
- 2016-03-22 OneZero团队 Daily Scrum Meeting
- 【Java】-NO.13.Algorithm.1.Java Algorithm.1.001-【Java 常用算法手册 】-
热门文章
- spring boot + mybatis + mybatis逆向工程 --- 心得
- SpringCloud的Config应用
- 使用Swing的GUI编程
- PAT 乙级 1001. 害死人不偿命的(3n+1)猜想 (15)(C语言描述)
- Ubuntu 18.04 server安装+搭建Seacms v10.1网站
- vue学习18-过滤器
- gin中的query和postform用法
- Java语法专题1: 类的构造顺序
- Java 继承02
- .NET 20周年软件趋势随想