将图像转换为单色字节数组
我正在编写一个库来将 C# 与 EPL2 打印机语言连接起来。规范文档说,我想尝试实现的一项功能是打印图像
p1 = 图形宽度 图形宽度(以字节为单位)。八 (8) 个点 = 一 (1) 个字节的数据。
p2 = 图形长度 图形长度(以点(或打印行)为单位)
数据 = 没有图形文件格式的原始二进制数据。数据必须以字节为单位。将宽度(以字节为单位) (p1) 乘以打印行数 (p2),即为图形数据总量。打印机根据此公式自动计算数据块的准确大小。
我计划将我的源图像设为每像素 1 位的 bmp 文件,并且已经缩放到尺寸。我只是不知道如何将其从该格式转换为字节[]以便我发送到打印机。我尝试了 ImageConverter.ConvertTo(Object, Type)
它成功了,但它输出的数组大小不正确,并且文档非常缺乏关于如何格式化输出的信息。
我当前的测试代码。
Bitmap i = (Bitmap)Bitmap.FromFile("test.bmp");
ImageConverter ic = new ImageConverter();
byte[] b = (byte[])ic.ConvertTo(i, typeof(byte[]));
即使方向完全不同,我们也非常感谢任何帮助。
I am writing a library to interface C# with the EPL2 printer language. One feature I would like to try to implement is printing images, the specification doc says
p1 = Width of graphic Width of graphic in bytes. Eight (8) dots = one (1) byte of data.
p2 = Length of graphic Length of graphic in dots (or print lines)
Data = Raw binary data without graphic file formatting. Data must be in bytes. Multiply the width in bytes (p1) by the number of print lines (p2) for the total amount of graphic data. The printer automatically calculates the exact size of the data block based upon this formula.
I plan on my source image being a 1 bit per pixel bmp file, already scaled to size. I just don't know how to get it from that format in to a byte[] for me to send off to the printer. I tried ImageConverter.ConvertTo(Object, Type)
it succeeds but the array it outputs is not the correct size and the documentation is very lacking on how the output is formatted.
My current test code.
Bitmap i = (Bitmap)Bitmap.FromFile("test.bmp");
ImageConverter ic = new ImageConverter();
byte[] b = (byte[])ic.ConvertTo(i, typeof(byte[]));
Any help is greatly appreciated even if it is in a totally different direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只需要将位图转换为字节数组,请尝试使用 MemoryStream:
查看此链接:C# 图像到字节数组和字节数组到图像转换器类
If you just need to convert your bitmap into a byte array, try using a MemoryStream:
Check out this link: C# Image to Byte Array and Byte Array to Image Converter Class
正如SLaks所说我需要使用LockBits
As SLaks said I needed to use LockBits