C# TSC打印二维码和条形码的实现方法

作者:叶长种 时间:2021-10-06 14:39:53 

效果图

C# TSC打印二维码和条形码的实现方法

C# TSC打印二维码和条形码的实现方法

开发、使用环境说明

安装TSC_7.3.8_M-3.exe打印机驱动,安装时选择对应的ttp 244 pro

将TSCLIB.dll复制到C:\Windows\system

驱动安装说明

C# TSC打印二维码和条形码的实现方法

选择下一步

C# TSC打印二维码和条形码的实现方法

选择安装路径,默认即可,选择下一步

C# TSC打印二维码和条形码的实现方法

选择安装打印机,选择下一步

C# TSC打印二维码和条形码的实现方法

选择其他,点击下一步

C# TSC打印二维码和条形码的实现方法

选择对应的打印机型号,点击下一步

C# TSC打印二维码和条形码的实现方法

选择USB端口,点击下一步

C# TSC打印二维码和条形码的实现方法

直接默认即可,点击下一步

驱动安装完成!

TSCLIB.cs代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace WindowsFormsPrint
{
public class TSCLIB_DLL
{
[DllImport("TSCLIB.dll", EntryPoint = "about")]
public static extern int about();
[DllImport("TSCLIB.dll", EntryPoint = "openport")]
public static extern int openport(string printername);
[DllImport("TSCLIB.dll", EntryPoint = "barcode")]
public static extern int barcode(string x, string y, string type,
  string height, string readable, string rotation,
  string narrow, string wide, string code);
[DllImport("TSCLIB.dll", EntryPoint = "clearbuffer")]
public static extern int clearbuffer();
[DllImport("TSCLIB.dll", EntryPoint = "closeport")]
public static extern int closeport();
[DllImport("TSCLIB.dll", EntryPoint = "downloadpcx")]
public static extern int downloadpcx(string filename, string image_name);
[DllImport("TSCLIB.dll", EntryPoint = "formfeed")]
public static extern int formfeed();
[DllImport("TSCLIB.dll", EntryPoint = "nobackfeed")]
public static extern int nobackfeed();
[DllImport("TSCLIB.dll", EntryPoint = "printerfont")]
public static extern int printerfont(string x, string y, string fonttype,
  string rotation, string xmul, string ymul,
  string text);
[DllImport("TSCLIB.dll", EntryPoint = "printlabel")]
public static extern int printlabel(string set, string copy);
[DllImport("TSCLIB.dll", EntryPoint = "sendcommand")]
public static extern int sendcommand(string printercommand);
[DllImport("TSCLIB.dll", EntryPoint = "setup")]
public static extern int setup(string width, string height,
  string speed, string density,
  string sensor, string vertical,
  string offset);
[DllImport("TSCLIB.dll", EntryPoint = "windowsfont")]
public static extern int windowsfont(int x, int y, int fontheight,
  int rotation, int fontstyle, int fontunderline,
  string szFaceName, string content);

//打开打印机端口,并进行相关设置
public static void openportExt()
{
 TSCLIB_DLL.openport("TSC TTP-244 Pro");//找打打印机端口
 TSCLIB_DLL.sendcommand("SIZE 60 mm,30 mm");//设置条码大小
 TSCLIB_DLL.sendcommand("GAP 2 mm,0");//设置条码间隙
 TSCLIB_DLL.sendcommand("SPEED 4");//设置打印速度
 TSCLIB_DLL.sendcommand("DENSITY 7");//设置墨汁浓度
 TSCLIB_DLL.sendcommand("DERECTION 1");//设置相对起点
 TSCLIB_DLL.sendcommand("REFERENCE 3 mm,3 mm");//设置偏移边框
 TSCLIB_DLL.sendcommand("CLS");//清除记忆(每次打印新的条码时先清除上一次的打印记忆)
}
//打印在用车档案二维码
public static void printVehicleCode(string str_hetong, string str_zhengjian, string str_name)
{
 char space = (char)(32);
 string codeValue = str_hetong + space + str_zhengjian;
 TSCLIB_DLL.sendcommand("CLS");//需要清除上一次的打印记忆
 TSCLIB_DLL.sendcommand("QRCODE 260,20,H,7,A,0,M2,S7,\"" + codeValue + "\"");
 TSCLIB_DLL.windowsfont(240, 100, 24, 180, 0, 0, "黑体", str_hetong);
 TSCLIB_DLL.windowsfont(240, 140, 24, 180, 0, 0, "黑体", str_zhengjian);
 TSCLIB_DLL.windowsfont(240, 180, 24, 180, 0, 0, "黑体", str_name);
 TSCLIB_DLL.printlabel("1", "1");
}
//打印财务条形码
public static void printFinanceCode(string str_Date, string str_siteNo,
 string str_siteName, string str_Num, string str_Name, int count)
{
 for (int i = 0; i < count; i++)
 {
 char Num = (char)(i + 65);
 char space = (char)(32);
 string value = str_Date + space + str_siteNo + space + str_Num + space + Num;
 string txt = str_Date + space + str_siteName + space + str_Num + space + str_Name + space + Num;
 TSCLIB_DLL.sendcommand("CLS");//需要清除上一次的打印记忆
 TSCLIB_DLL.barcode("40", "50", "128", "160", "0", "0", "2", "2", value);
 TSCLIB_DLL.windowsfont(440, 35, 24, 180, 0, 0, "黑体", txt);
 TSCLIB_DLL.printlabel("1", "1");
 }
}
//关闭打印机端口
public static void closeportExt()
{
 TSCLIB_DLL.closeport();
}
}
}

打印二维码:


private void print_Click(object sender, EventArgs e)
{
 try
 {
 print.Enabled = false;
 base.DoWork(DoPrint);
 print.Enabled = true;
 }
 catch (Exception ex)
 {
 // FileLogHelper.WriteInfoLog(ex.ToString());
 MessageBox.Show(ex.ToString());
 }
}
private void DoPrint(object sender, EventArgs e)
{
 DataGridViewSelectedRowCollection rows = ArchivesView.SelectedRows;
 if (rows.Count <= 0)
 {
 MessageBox.Show("请先选择数据项!");
 return;
 }
 TSCLIB_DLL.openportExt();
 foreach (DataGridViewRow dr in rows)
 {
 TSCLIB_DLL.printVehicleCode(dr.Cells[1].Value.ToString(), dr.Cells[2].Value.ToString(), dr.Cells[3].Value.ToString());
 }
 TSCLIB_DLL.closeportExt();
}

C#调用dll提示"试图加载格式不正确的程序"解决方法

程序在32位操作系统上运行正常,在64位操作系统上运行读卡功能提示”试图加载格式不正确“。

程序在64位操作系统上运行正常,在64位操作系统上运行读卡功能提示”试图加载格式不正确“。

--------------------------------------------------------------------------------------------

点击项目属性,把目标平台Any CPU 设置为X86(32位操作系统)或者X64(64位操作系统)

C# TSC打印二维码和条形码的实现方法

按照上面解决方案可能会有下面的问题:

C# TSC打印二维码和条形码的实现方法

C# form继承问题 : 提示 无法加载基类 ;请确保已引用该程序集并已生成所有项目。

把 生成 - 目标平台 改成 x86或者Any CPU ,重新生成一次,Form可正常编辑。

所以新的解决方案是:选择Any CPU 把下面打勾的去掉

C# TSC打印二维码和条形码的实现方法

来源:http://www.cnblogs.com/yechangzhong-826217795/p/7216352.html

标签:C#,TSC,打印,二维码,条形码
0
投稿

猜你喜欢

  • C#中文件名或文件路径非法字符判断方法

    2023-12-01 14:14:21
  • Java实现查找算法的示例代码(二分查找、插值查找、斐波那契查找)

    2022-05-28 04:17:38
  • 解析Java的Hibernate框架中的持久化类和映射文件

    2023-08-19 15:00:05
  • C#中泛型容器Stack<T>的用法并实现”撤销/重做”功能

    2021-06-27 04:12:00
  • 浅析C# 结构体struct

    2021-08-08 08:21:19
  • C#中闭包概念讲解

    2022-08-16 05:16:28
  • 深入C#中使用SqlDbType.Xml类型参数的使用详解

    2022-07-24 13:56:18
  • Java中泛型的用法总结

    2023-03-03 08:54:47
  • springboot 自定义异常并捕获异常返给前端的实现代码

    2022-07-23 03:09:52
  • 详解Android自定义View--自定义柱状图

    2023-10-13 19:33:06
  • Spring Boot利用Docker快速部署项目的完整步骤

    2022-03-08 18:52:55
  • Android获取内外置存储卡的方法

    2023-12-03 00:41:09
  • Java使用DateFormatter格式化日期时间的方法示例

    2021-07-15 15:29:23
  • IDEA社区版下载安装流程详解(小白篇)

    2021-11-13 12:37:18
  • Java实现获取指定个数的不同随机数

    2023-11-14 21:42:34
  • android压力测试命令monkey详解

    2023-06-17 00:36:29
  • 剑指Offer之Java算法习题精讲链表与字符串及数组

    2022-10-03 19:10:17
  • java中unicode和中文相互转换的简单实现

    2021-12-18 09:46:17
  • JavaWeb框架MVC设计思想详解

    2022-09-09 06:43:50
  • 浅谈Java代码的 微信长链转短链接口使用 post 请求封装Json(实例)

    2023-07-27 19:36:09
  • asp之家 软件编程 m.aspxhome.com