C# Lpt 并口热敏小票打印机打印位图
2024-08-26 10:56:25
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");
}
最新文章
- [Android]依赖注入框架squareup的dagger
- OpenCV2:等间隔采样和局部均值的图像缩小
- Linux 挂载U盘
- setAttribute,,,getAttribute,,,,
- 关于WEB Service&;WCF&;WebApi实现身份验证之WCF篇(2)
- Windows Azure文件共享服务--File Service
- 繁简体 互转 js
- Newtonsoft 自定义输出内容
- Max Sub-matrix
- c++ 学习之const专题之const成员函数
- 更新cydia“sub-process/usr/libexec/cydia/cydo returned anerror code(2)”是怎么回事?
- CodeForces 76E Points
- 使用TP5创建一个REST API
- ueditor精简插件和减少初次加载文件的方法
- CSS实现文字换行
- ionic创建项目失败
- usb帧格式
- Linux基础命令---yes
- 在C#客户端用HTTP上传文件到Java服务器
- Android TextEdit属性
热门文章
- (判断url文件大小)关于inputStream.available()方法获取下载文件的总大小
- CSS margin-top 属性
- Endnote在latex中的应用的两种方法
- [转]sa不能远程连接sql server 2008的解决办法
- iOS:进度条控件的详细使用
- http://www.cnblogs.com/ITtangtang/archive/2012/05/21/2511749.html
- Strategy Pattern(策略模式)
- git remote branch操作
- [转]JQuery - Sizzle选择器引擎原理分析
- java运行shell命令,chmod 777 xxx,改变权限无效的解决的方法。