public string GetLogo()
{
string logo = "";
if (!File.Exists(@"C:\bitmap.bmp"))
return null;
BitmapData data = GetBitmapData(@"C:\bitmap.bmp");
BitArray dots = data.Dots;
byte[] width = BitConverter.GetBytes(data.Width);
int offset = 0;
MemoryStream stream = new MemoryStream();
BinaryWriter bw = new BinaryWriter(stream);
bw.Write((char)0x1B);
bw.Write('@');
bw.Write((char)0x1B);
bw.Write('3');
bw.Write((byte)24);
while (offset < data.Height)
{
bw.Write((char)0x1B);
bw.Write('*');
bw.Write((byte)33);
bw.Write(width[0]);
bw.Write(width[1]);
for (int x = 0; x < data.Width; ++x)
{
for (int k = 0; k < 3; ++k)
{
byte slice = 0;
for (int b = 0; b < 8; ++b)
{
int y = (((offset / 8) + k) * 8) + b;
int i = (y * data.Width) + x;
bool v = false;
if (i < dots.Length)
{
v = dots[i];
}
slice |= (byte)((v ? 1 : 0) << (7 - b));
}
bw.Write(slice);
}
}
offset += 24;
bw.Write((char)0x0A);
}
bw.Write((char)0x1B);
bw.Write('3');
bw.Write((byte)30);
bw.Flush();
byte[] bytes = stream.ToArray();
return logo + Encoding.Default.GetString(bytes);
}
public BitmapData GetBitmapData(string bmpFileName)
{
using (var bitmap = (Bitmap)Bitmap.FromFile(bmpFileName))
{
var threshold = 127;
var index = 0;
double multiplier = 570;
double scale = (double)(multiplier/(double)bitmap.Width);
int xheight = (int)(bitmap.Height * scale);int xwidth =(int)(bitmap.Width* scale);var dimensions = xwidth * xheight;var dots =newBitArray(dimensions);for(var y =0; y < xheight; y++){for(var x =0; x < xwidth; x++){var _x =(int)(x / scale);var _y =(int)(y / scale);var color = bitmap.GetPixel(_x, _y);var luminance =(int)(color.R *0.3+ color.G *0.59+ color.B *0.11);
dots[index]=(luminance < threshold);
index++;}}returnnewBitmapData(){Dots= dots,Height=(int)(bitmap.Height*scale),Width=(int)(bitmap.Width*scale)};}}publicclassBitmapData{publicBitArrayDots{
get;set;}publicintHeight{
get;set;}publicintWidth{
get;set;}}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步