[英]Bitmap to byte array conversion
I need to convert a bitmap image to a byte array and print it using thermal printer where the top pixel of each column is the MSB and the bottom pixel is the LSB. 我需要将位图图像转换为字节数组,然后使用热敏打印机进行打印,其中每列的顶部像素是MSB,底部像素是LSB。 Currently, my implementation is like this but it does not print the image as expected:
当前,我的实现是这样的,但是它不能按预期打印图像:
Bitmap bitmap = (Bitmap)this.PictureBoxOriginalBitmap.Image;
int width, height;
width = bitmap.Width;
height = bitmap.Height;
byte[] data = new byte[height * width / 8];
uint counter = 0, index = 0;
byte[] sequence = new byte[8] { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
for (int rowIndex = 0; rowIndex < height / 8; rowIndex++)
{
for (int columnIndex = 0; columnIndex < width; columnIndex++)
{
for (int pixelColumn = rowIndex * 8; pixelColumn < (rowIndex + 1) *8; pixelColumn++)
{
data[counter] |= (byte)((bitmap.GetPixel(columnIndex, pixelColumn).R == 255) ? 0x00 : sequence[index]);
if (++index == 8)
{
counter++;
index = 0;
}
}
}
}
StringBuilder message = new StringBuilder();
byte[] buffer;
this._bitmapInfo = new BitmapInformation();
buffer = new byte[width];
for (int rowIndex = 0; rowIndex < height / 8; rowIndex++)
{
Array.Copy(data, width * rowIndex, buffer, 0, width);
message.AppendLine(Helper.byteAsStringWith0x(buffer, true).Trim());
this._bitmapInfo[rowIndex] = Helper.byteAsStringWith0x(buffer,true).Trim();
}
Refactored your solution a bit. 重构了您的解决方案。 Was quiet hard to figure out what was going on.
很难安静地弄清楚发生了什么事。 Tested my solution, on a small bitmap, and it seems that my solution is doing the correct thing.
在一个小的位图上测试了我的解决方案,看来我的解决方案在做正确的事情。 At least on your requirement.
至少在您的要求下。
Bitmap bitmap = (Bitmap)this.PictureBoxOriginalBitmap.Image;
var width = bitmap.Width;
var height = bitmap.Height;
var shiftEnumerable = Enumerable.Range(0, 8).Reverse(); // create an array from (7..0) MSB as the first
var dataList = new List<byte>(); // Create a list instead of fiddeling with array indexes
for (int rowIndex = 7; rowIndex < height; rowIndex=rowIndex+8 ) // increment 8 pixels each iteration on y-axis. If picture is less than 8 pixels, convertion will not happen.
{
for (int columnIndex = 0; columnIndex < width; columnIndex++) // increment x-axis
{
// Linq which performs a bitwise OR operation. Converts the predicate to byte and shifts result x places depending on which bit needs to be set.
// Add the accumulated OR operation to a list of bytes.
dataList.Add(shiftEnumerable.Aggregate<int, byte>(
0,
(accumulate, current) => (byte)(accumulate | (byte)(Convert.ToByte(bitmap.GetPixel(columnIndex, rowIndex - current).R == 255) << current))));
}
}
// Convert List to array.
var data = dataList.ToArray();
Did this because it was a fun exercise for me. 这样做是因为对我来说这是一个有趣的练习。 Please let me know if you can use it :-)
请让我知道是否可以使用它:-)
EDIT: 编辑:
idx: New | Old
-----------------------
[0]: 170 | 170
[1]: 255 | 0
[2]: 127 | 1
[3]: 63 | 3
[4]: 31 | 7
[5]: 15 | 15
[6]: 7 | 31
[7]: 3 | 63
[8]: 129 | 126
[9]: 192 | 252
[10]: 224 | 248
[11]: 240 | 240
[12]: 248 | 224
[13]: 252 | 192
[14]: 254 | 128
[15]: 255 | 0
Tried to compare the output of the the data[]
array, and as you can see. 如您所见,试图比较
data[]
数组的输出。 Our results are quite different. 我们的结果是完全不同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.