原文:
C#并口热敏小票打印机打印位图
最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了。
这是DEMO的窗体:
下面是打印所需要调用的代码:
class LptControl
{
private string LptStr = "lpt1";
public LptControl(string l_LPT_Str)
{
LptStr = l_LPT_Str;
}
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
//调用DLL.
[DllImport("kernel32.dll")]
private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, ref OVERLAPPED lpOverlapped);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(int hObject);
private int iHandle;
/// <summary>
/// 打开端口
/// </summary>
/// <returns></returns>
public bool Open()
{
iHandle = CreateFile(LptStr, 0x40000000, 0, 0, 3, 0, 0);
// iHandle = CreateFile(LptStr, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 打印字符串,通过调用该方法可以打印需要的字符串
/// </summary>
/// <param name="Mystring"></param>
/// <returns></returns>
public bool Write(String Mystring)
{
//如果端口为打开,则提示,打开,则打印
if (iHandle != -1)
{
OVERLAPPED x = new OVERLAPPED();
int i = 0;
//byte[] mybyte = System.Text.Encoding.Defau