【发布时间】:2012-11-22 20:22:33
【问题描述】:
参考链接:
更新
我现在使用 LockBits 更改了图像转换,但失败了。谁能给我一些建议?
/// <summary>
/// Orginal command Format in Decimal:
/// 29[GS], 40[(], 76[L], pL, PH, 48(m), 67[fn], 48[a], kc1, kc2, b, xL, xH, yL, yH, c, [d1 -- dk]
/// pL = Lower bit of sum of parameters (m to dk) = 11(m to c) + last bit of image
/// pH = Higher bit of sum of parameters (m to dk) = 11(m to c) + last bit of image
/// kc = Key Code of NVRam, kc1 = first bit of key code, kc2 = second bit of key code. P.S.: The key code will be hardcode H1.
/// b = number of colors = 1
/// xL & xH = Lower bit of image width, e.g. Width = 128 = 0x0080 then xL = 0x80, xH = 0x00
/// </summary>
/// <param name="pLogo"></param>
public void LoadImageToPrinter(Bitmap pLogo)
{
BitmapData oBmpData = pLogo.LockBits(new Rectangle(0, 0, pLogo.Width, pLogo.Height), ImageLockMode.ReadOnly, pLogo.PixelFormat);
//The list contains all the commands in Decimal format
List<int> oCommandList = new List<int>();
//k = (int((xL + xH × 256) + 7)/8) × (yL + yH × 256)
string HexValueOfX = pLogo.Width.ToString("X4");
string HexValueOfY = pLogo.Height.ToString("X4");
//k
int oExpectedImageByteCount = Math.Abs(oBmpData.Stride) * pLogo.Height;
//Total bit used for parameters
int oTotalParameterBitCount = 11 + oExpectedImageByteCount;
//The hex value for the oTotalParameterBitCount
string oTotalParameterBitCountInHex = oTotalParameterBitCount.ToString("X4").PadLeft(4, '0');
//GS, (, L
oCommandList.AddRange(new int[] { 29, 40, 76 });
//pL
oCommandList.Add(GetLowerHexValue(oTotalParameterBitCountInHex));
//pH
oCommandList.Add(GetHigherHexValue(oTotalParameterBitCountInHex));
//m, fn, a, kc1, kc2, b
oCommandList.AddRange(new int[] { 48, 67, 48, (int)'H', (int)'1', 1 });
//xL, xH
oCommandList.AddRange(new int[] { GetLowerHexValue(HexValueOfX), GetHigherHexValue(HexValueOfX) });
//yL, yH, c
oCommandList.AddRange(new int[] { GetLowerHexValue(HexValueOfY), GetHigherHexValue(HexValueOfY), 49 });
//Append the image bit to the List
byte[] oImageByte = new byte[oExpectedImageByteCount];
IntPtr oPtr = oBmpData.Scan0;
Marshal.Copy(oPtr, oImageByte, 0, oExpectedImageByteCount);
pLogo.UnlockBits(oBmpData);
//Clear NVRam
mThermalPrinterLibrary.Write(new int[] { 29, 40, 76, 5, 0, 48, 65, 67, 76, 82 }.IntArrayToCharString());
//Store graphics data
mThermalPrinterLibrary.Write(oCommandList.ToArray().IntArrayToCharString());
mThermalPrinterLibrary.Write(oImageByte);
//Print the graphics data
mThermalPrinterLibrary.Write(new int[] { 29, 40, 76, 6, 0, 48, 69, (int)'H', (int)'1', 1, 1 }.IntArrayToCharString());
}
【问题讨论】:
-
您似乎在为每个像素添加一个字节:这是错误的;每个字节代表 8 个像素。您还命名了一个变量“oExpectedImageBitCount”。 'oExpectedImageByteCount' 会更好。
-
更新了变量名,但我仍然不知道如何以正确的方式将位图转换为字节数组,对不起。之前没有任何图像处理经验,我应该在谷歌或其他地方寻找什么?谢谢
-
这不是特定于图像处理的,只是位操作。您似乎有获取像素的代码(pLogo.GetPixel)。输出的每个字节代表 8 个像素:0x80 是第一个,0x40 是第二个,... 0x01 是第八个。您只需要一次处理 8 个像素并适当设置位的代码,例如使用位移运算符 (
-
您能向我解释一下第二个参考链接的表格吗?谢谢
-
两个链接似乎都坏了:-(
标签: c# .net printing command epson