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, , , , , );
// iHandle = CreateFile(LptStr, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if (iHandle != -)
{
return true;
}
else
{
return false;
}
} /// <summary>
/// 打印字符串,通过调用该方法可以打印需要的字符串
/// </summary>
/// <param name="Mystring"></param>
/// <returns></returns>
public bool Write(String Mystring)
{
//如果端口为打开,则提示,打开,则打印
if (iHandle != -)
{
OVERLAPPED x = new OVERLAPPED();
int i = ;
//byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
byte[] mybyte = Encoding.GetEncoding("GB2312").GetBytes(Mystring);
bool b = WriteFile(iHandle, mybyte, mybyte.Length, ref i, ref x);
return b;
}
else
{
throw new Exception("不能连接到打印机!");
}
}
/// <summary>
/// 打印命令,通过参数,可以打印小票打印机的一些命令,比如换行,行间距,打印位图等。
/// </summary>
/// <param name="mybyte"></param>
/// <returns></returns>
public bool Write(byte[] mybyte)
{
//如果端口为打开,则提示,打开,则打印
if (iHandle != -)
{
OVERLAPPED x = new OVERLAPPED();
int i = ;
return WriteFile(iHandle, mybyte, mybyte.Length, ref i, ref x);
}
else
{
throw new Exception("不能连接到打印机!");
}
} /// <summary>
/// 关闭端口
/// </summary>
/// <returns></returns>
public bool Close()
{
return CloseHandle(iHandle);
} }
 /// <summary>
/// 打印图片方法
/// </summary>
public void PrintOne()
{
//获取图片
Bitmap bmp = new Bitmap(pictureBox1.Image); //设置字符行间距为n点行
//byte[] data = new byte[] { 0x1B, 0x33, 0x00 };
string send = "" + (char)() + (char)() + (char)();
byte[] data = new byte[send.Length];
for (int i = ; i < send.Length; i++)
{
data[i] = (byte)send[i];
}
lc.Write(data); data[] = (byte)'\x00';
data[] = (byte)'\x00';
data[] = (byte)'\x00'; // Clear to Zero. Color pixelColor; //ESC * m nL nH d1…dk 选择位图模式
// ESC * m nL nH
byte[] escBmp = new byte[] { 0x1B, 0x2A, 0x00, 0x00, 0x00 }; escBmp[] = (byte)'\x21'; //nL, nH
escBmp[] = (byte)(bmp.Width % );
escBmp[] = (byte)(bmp.Width / ); //循环图片像素打印图片
//循环高
for (int i = ; i < (bmp.Height / + ); i++)
{
//设置模式为位图模式
lc.Write(escBmp);
//循环宽
for (int j = ; j < bmp.Width; j++)
{
for (int k = ; k < ; k++)
{
if (((i * ) + k) < bmp.Height) // if within the BMP size
{
pixelColor = bmp.GetPixel(j, (i * ) + k);
if (pixelColor.R == )
{
data[k / ] += (byte)( >> (k % )); }
}
}
//一次写入一个data,24个像素
lc.Write(data); data[] = (byte)'\x00';
data[] = (byte)'\x00';
data[] = (byte)'\x00'; // Clear to Zero.
} //换行,打印第二行
byte[] data2 = { 0xA };
lc.Write(data2);
} // data
lc.Write("\n\n");
}

最新文章

  1. [Android]依赖注入框架squareup的dagger
  2. OpenCV2:等间隔采样和局部均值的图像缩小
  3. Linux 挂载U盘
  4. setAttribute,,,getAttribute,,,,
  5. 关于WEB Service&amp;WCF&amp;WebApi实现身份验证之WCF篇(2)
  6. Windows Azure文件共享服务--File Service
  7. 繁简体 互转 js
  8. Newtonsoft 自定义输出内容
  9. Max Sub-matrix
  10. c++ 学习之const专题之const成员函数
  11. 更新cydia“sub-process/usr/libexec/cydia/cydo returned anerror code(2)”是怎么回事?
  12. CodeForces 76E Points
  13. 使用TP5创建一个REST API
  14. ueditor精简插件和减少初次加载文件的方法
  15. CSS实现文字换行
  16. ionic创建项目失败
  17. usb帧格式
  18. Linux基础命令---yes
  19. 在C#客户端用HTTP上传文件到Java服务器
  20. Android TextEdit属性

热门文章

  1. (判断url文件大小)关于inputStream.available()方法获取下载文件的总大小
  2. CSS margin-top 属性
  3. Endnote在latex中的应用的两种方法
  4. [转]sa不能远程连接sql server 2008的解决办法
  5. iOS:进度条控件的详细使用
  6. http://www.cnblogs.com/ITtangtang/archive/2012/05/21/2511749.html
  7. Strategy Pattern(策略模式)
  8. git remote branch操作
  9. [转]JQuery - Sizzle选择器引擎原理分析
  10. java运行shell命令,chmod 777 xxx,改变权限无效的解决的方法。