C#生成单色bmp图片,转为单色bmp图片 任意语言完全用字节拼一张单色图,LCD取模 其它格式图片转为单色图

news/2024/10/20 7:51:54/

最终效果:

V1.8.2 20230419
文字生成单色BMP图片4.exe
默认1280*720  如果显示不全,请把宽和高加大  字体加大。

 

首先,用windows画板生成一张1*1白色单色图作为标准,数据如下:

数据解析参考:BMP图像文件完全解析 - 知乎

单色图有点不一样的是像数数据部分,是1bit一个点,0黑1白。4字节对齐是一样的。 比如上面是8000 0000   ,80即二进制1000 0000。因为是1*1,只有一个点有效,其它是4字节对齐。

 再建一个2*1,变成C0    即二进制1100 0000

 再建一个2*1,变成C0    即二进制1100 0000

  再建一个1*2,变成8个数据,变成8000 0000 8000 0000 每一列都需要凑4字节的倍数

用C#生成单色图:

private void Form1_Load(object sender, EventArgs e){Bitmap bmp2 = new Bitmap(1, 1, PixelFormat.Format1bppIndexed);//其它bmp格式的画图数据//Graphics _Graphics = Graphics.FromImage(bmp2);//_Graphics.Clear(Color.Red);//_Graphics.Dispose();//bmp2.SetResolution( 72,72);//单色图数据Rectangle rect = new Rectangle(0, 0, bmp2.Width, bmp2.Height);System.Drawing.Imaging.BitmapData bmpData = bmp2.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp2.PixelFormat);// Get the address of the first line.IntPtr ptr = bmpData.Scan0;// Declare an array to hold the bytes of the bitmap.int bytes = Math.Abs(bmpData.Stride) * bmp2.Height;//error if bmpData.Widthbyte[] rgbValues = new byte[bytes];// Copy the RGB values into the array.System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);//Generate dots in random cells and show imagefor (int i = 0; i < bmp2.Height; i++){for (int j = 0; j < bmp2.Width; j += 8){byte b = 0;b = (byte)(b | ((byte)(1) << 7));//b = (byte)(b | ((byte)(1) << 6));//b = (byte)(b | ((byte)(1) << 5));//b = (byte)(b | ((byte)(1) << 4));//b = (byte)(b | ((byte)(1) << 3));//b = (byte)(b | ((byte)(1) << 2));//b = (byte)(b | ((byte)(1) << 1));//b = (byte)(b | ((byte)(1) << 0));rgbValues[i * bmpData.Stride + j / 8] = (byte)b;}}// Copy back values into the array.System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);// Unlock the bits.bmp2.UnlockBits(bmpData);//save bmpbmp2.Save("Test.bmp",ImageFormat.Bmp);}

 任意语言完全用字节拼一张图:

 JAVA C# Zxing生成的二维码数据转换为1bit的bmp下发到点阵终端。QRCode去白边,以bmp格式字节流发送,BMP图片解析_java图片转点阵_小黄人软件的博客-CSDN博客

谁有空帮填一下就行。JAVA写的部分。 

				final int[] pixels = new int[width * height]; //假设有这么多图像数据,比如二维码矩阵final byte[] pixelsByte = new byte[ pixels.length/8 ]; //8个pixels合并到一个pixelsByte字节final byte[] bmpFile = new byte[ 0x3E+pixels.length/8 ]; //图片存储的位置+像素数据大小 整个文件buffArrays.fill(bmpFile, (byte) 0); //默认为0//0~1字节42、4d为B、M字符,表示BMP文件//2~5字节表示整个BMP文件的大小,小端模式,即0x0000003a,58字节//6~9字节是保留数据,一般都是0//10~13字节表示图片存储的位置//14~17字节为位图信息数据头,一般是40,即0x00000028。//18~21字节表示图像宽度,即0x00000001,//22~25字节表示图像高度。//26~27字节表示色彩平面数量,必须为1,即0x0001//28~29字节表示每个像素存储的位数(蓝色部分,0x0018,即24位)。//30~33字节为压缩方式,0表示不压缩//34~37字节表示原始位图数据的大小,即0x00000004,即4字节//38~41字节表示横向分辨率//42~45字节表示纵向分辨率//46~49字节表示调色板颜色数//50~53字节表示重要颜色数//54~57字节(红色部分)即原始的像素数据,这些才是最终需要显示到屏幕上的数据bmpFile[0]=0x42; //‘B’ bmpFile[1]=0x4d; //'M'bmpFile[10]=0x3E; //图片存储的位置System.arraycopy(pixelsBit,0,bmpFile,bmpFile[10],pixelsBit.length); //图像数据复制到buf位置WriteToFile(bmpFile,bmpFile.length);//二进制写入到文件

 其它图片转为单色图:

 public void generateBMP(string contents, int width = 1280, int height = 720){if (contents.Length > 0){using (Bitmap bmp = GetStrBMPALL(contents, width, height)) // 获取待分析的字符位图{pictureBox2.Image = ConvertTo24bppTo1bpp(bmp);}}else{pictureBox2.Image = null;}pictureBox2.BringToFront(); //最前显示System.GC.Collect();//清内存 不然图片一直增加内存}//白底黑字public static Bitmap GetStrBMPALL(string str, int bmpWidth = 1280, int bmpHeight = 720) //str字符串  width单个宽度  height单个高度{str = str.Replace("\r", "");string[] lineList = str.Split('\n');Bitmap bmp = new Bitmap(bmpWidth, bmpHeight); // 新建位图变量Graphics g = Graphics.FromImage(bmp);Brush backgroud = Brushes.White; //点阵分隔线颜色g.FillRectangle(backgroud, new Rectangle(0, 0, bmpWidth, bmpHeight));//实心正方形 背景int y = 0;foreach (string line in lineList){int widthTemp = 0;foreach (char ch in line){g.DrawString(ch.ToString(), p.myFont, Brushes.Black, new PointF(widthTemp + p.xOffset, y + p.yOffset + 0.0F)); //new Font("宋体", 10)if ((int)ch <= 127) { widthTemp += p.width / 2; } else { widthTemp += p.width; } //如果是字符ASCII,宽度为8 如果是汉字,宽度为16}y += p.height;}//bmp.Save("out32.bmp");return bmp;}public static Bitmap ConvertTo24bppTo1bpp(Bitmap bmp, int pixelSize=1, int jg=0,int width=1280,int height=720) //bmp要显示的图  pixelSize单个点大小   jg点与点间隔 ,返回转换后的图{Bitmap bmp2 = new Bitmap(width, height, PixelFormat.Format1bppIndexed);         //Bitmap bmp2 = new Bitmap(@"Test.bmp", true);Rectangle rect = new Rectangle(0, 0, bmp2.Width, bmp2.Height);System.Drawing.Imaging.BitmapData bmpData = bmp2.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp2.PixelFormat);// Get the address of the first line.IntPtr ptr = bmpData.Scan0;// Declare an array to hold the bytes of the bitmap.int bytes = Math.Abs(bmpData.Stride) * bmp2.Height;//error if bmpData.Widthbyte[] rgbValues = new byte[bytes];// Copy the RGB values into the array.System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);//Generate dots in random cells and show imagefor (int i = 0; i < bmp.Height; i++){for (int j = 0; j < bmp.Width; j += 8){byte b = 0;b = (byte)(b | ((byte)((bmp.GetPixel(j + 0, i) == Color.FromArgb(0x0, 0, 0)) ? 1 : 0) << 7));b = (byte)(b | ((byte)((bmp.GetPixel(j + 1, i) == Color.FromArgb(0x0, 0, 0)) ? 1 : 0) << 6));b = (byte)(b | ((byte)((bmp.GetPixel(j + 2, i) == Color.FromArgb(0x0, 0, 0)) ? 1 : 0) << 5));b = (byte)(b | ((byte)((bmp.GetPixel(j + 3, i) == Color.FromArgb(0x0, 0, 0)) ? 1 : 0) << 4));b = (byte)(b | ((byte)((bmp.GetPixel(j + 4, i) == Color.FromArgb(0x0, 0, 0)) ? 1 : 0) << 3));b = (byte)(b | ((byte)((bmp.GetPixel(j + 5, i) == Color.FromArgb(0x0, 0, 0)) ? 1 : 0) << 2));b = (byte)(b | ((byte)((bmp.GetPixel(j + 6, i) == Color.FromArgb(0x0, 0, 0)) ? 1 : 0) << 1));b = (byte)(b | ((byte)((bmp.GetPixel(j + 7, i) == Color.FromArgb(0x0, 0, 0)) ? 1 : 0) << 0));rgbValues[i * bmpData.Stride + j / 8] = (byte)b;}}// Copy back values into the array.System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);// Unlock the bits.bmp2.UnlockBits(bmpData);bmp2.Save("out.bmp", ImageFormat.Bmp);return bmp2;}private void button1_Click(object sender, EventArgs e){p.xOffset = Convert.ToInt32(textBox5.Text);p.yOffset = Convert.ToInt32(textBox6.Text);p.width = Convert.ToInt32(textBox3.Text);p.height = Convert.ToInt32(textBox4.Text);generateBMP(textBox1.Text);}


http://www.ppmy.cn/news/48389.html

相关文章

nodejs+vue 智慧餐厅点餐餐桌预订系统

现在社会的生活节奏越来越快&#xff0c;人们对互联网的需求也越来越大&#xff0c;不仅要求使用方便&#xff0c;而且对于功能及扩展性也有了更高的要求&#xff0c;最能达到要求莫过于利用计算机网络&#xff0c;将所需功能要求和计算机网络结合起来&#xff0c;就形成了本智…

【录用案例】1区SCI仅36天录用,新增多本1-2区SCI,CNKI评职好刊发表案例

我处上周&#xff08;2023年4月8日-2023年4月14日&#xff09;经核实&#xff0c;由我处Unionpub学术推荐的24篇论文已被期刊部录用、20篇见刊&#xff0c;5篇检索&#xff1a; ✔新增1区纳米与环境类SCI&EI&#xff0c;仅36天录用&#xff0c;录用后17天见刊&#xff1b;…

领域驱动设计(DDD)

目录 领域驱动设计(DDD)架构演进 DDD的几种典型架构 领域驱动设计(DDD)架构演进 IAAS:基础设施服务,Infrastructure-as-a-service。PAAS:平台服务,Platform-as-a-service。SAAS:软件服务,Software-as-a-service。 图架构的演进过程,通过对三个层的举例来进行说明…

ThreadLocal 内存泄露的原因及处理方式

1、ThreadLocal 使用原理 ThreadLocal的主要用途是实现线程间变量的隔离&#xff0c;表面上他们使用的是同一个ThreadLocal&#xff0c; 但是实际上使用的值value却是自己独有的一份。用一图直接表示threadlocal 的使用方式。 从图中我们可以当线程使用threadlocal 时&#xf…

使用MyBatis实现简单查询

文章目录 一&#xff0c;创建数据库与表&#xff08;一&#xff09;在Navicat里创建MySQL数据库testdb&#xff08;二&#xff09;创建用户表 - t_user&#xff08;三&#xff09;在用户表里插入3条记录 二&#xff0c;案例演示MyBatis基本使用&#xff08;一&#xff09;创建M…

JavaScript(JS)-1.JS基础知识

1.JavaScript概念 (1)JavaScript是一门跨平台&#xff0c;面向对象的脚本语言&#xff0c;来控制网页行为的&#xff0c;它能使网页可交互 (2)W3C标准&#xff1a;网页主要由三部分组成 ①结构&#xff1a;HTML负责网页的基本结构&#xff08;页面元素和内容&#xff09;。 …

[oeasy]python0136_接收输入_input函数_字符串_str

输入变量 回忆上次内容 上次研究了 一行赋值多个变量 a b 5a, b 7, 8 还研究了 标识符的惯用法 python使用的是 snake_case蛇形命名法用下划线 分隔开小写字母的 方法这样就可以 更合理地 命名变量了 变量变量 能变的量我可以 手工输入变量的值 吗&#xff1f;&#x1f9…

解决若依验证码异常:Error: image == null

前言 前两天在改项目突然发现若依的框架可以正常启动但是验证码加载不出来了&#xff0c;一直弹窗提示异常信息&#xff0c;下边是关于问题的描述和解决方案&#xff0c;没有耐心看过程的建议直接滑到最底下看解决方式 问题原因 登录页面一直提示 image null 如图 1 所示&…