将图像转换为单色字节数组

发布于 2024-08-27 15:27:12 字数 765 浏览 6 评论 0原文

我正在编写一个库来将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别靠近我心 2024-09-03 15:27:13

如果您只需要将位图转换为字节数组,请尝试使用 MemoryStream:

查看此链接:C# 图像到字节数组和字节数组到图像转换器类

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray();
}

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

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray();
}
手长情犹 2024-09-03 15:27:13

正如SLaks所说我需要使用LockBits

Rectangle rect = new Rectangle(0, 0, Bitmap.Width, Bitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = null;
byte[] bitVaues = null;
int stride = 0;
try
{
    bmpData = Bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, Bitmap.PixelFormat);
    IntPtr ptr = bmpData.Scan0;
    stride = bmpData.Stride;
    int bytes = bmpData.Stride * Bitmap.Height;
    bitVaues = new byte[bytes];
    System.Runtime.InteropServices.Marshal.Copy(ptr, bitVaues, 0, bytes);
}
finally
{
    if (bmpData != null)
        Bitmap.UnlockBits(bmpData);
}

As SLaks said I needed to use LockBits

Rectangle rect = new Rectangle(0, 0, Bitmap.Width, Bitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = null;
byte[] bitVaues = null;
int stride = 0;
try
{
    bmpData = Bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, Bitmap.PixelFormat);
    IntPtr ptr = bmpData.Scan0;
    stride = bmpData.Stride;
    int bytes = bmpData.Stride * Bitmap.Height;
    bitVaues = new byte[bytes];
    System.Runtime.InteropServices.Marshal.Copy(ptr, bitVaues, 0, bytes);
}
finally
{
    if (bmpData != null)
        Bitmap.UnlockBits(bmpData);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文