C# TSC打印二維碼和條形碼的實(shí)現(xiàn)方法
效果圖


開發(fā)、使用環(huán)境說明
安裝TSC_7.3.8_M-3.exe打印機(jī)驅(qū)動(dòng),安裝時(shí)選擇對應(yīng)的ttp 244 pro
將TSCLIB.dll復(fù)制到C:\Windows\system
驅(qū)動(dòng)安裝說明

選擇下一步

選擇安裝路徑,默認(rèn)即可,選擇下一步

選擇安裝打印機(jī),選擇下一步

選擇其他,點(diǎn)擊下一步

選擇對應(yīng)的打印機(jī)型號,點(diǎn)擊下一步

選擇USB端口,點(diǎn)擊下一步

直接默認(rèn)即可,點(diǎn)擊下一步
驅(qū)動(dòng)安裝完成!
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);
//打開打印機(jī)端口,并進(jìn)行相關(guān)設(shè)置
public static void openportExt()
{
TSCLIB_DLL.openport("TSC TTP-244 Pro");//找打打印機(jī)端口
TSCLIB_DLL.sendcommand("SIZE 60 mm,30 mm");//設(shè)置條碼大小
TSCLIB_DLL.sendcommand("GAP 2 mm,0");//設(shè)置條碼間隙
TSCLIB_DLL.sendcommand("SPEED 4");//設(shè)置打印速度
TSCLIB_DLL.sendcommand("DENSITY 7");//設(shè)置墨汁濃度
TSCLIB_DLL.sendcommand("DERECTION 1");//設(shè)置相對起點(diǎn)
TSCLIB_DLL.sendcommand("REFERENCE 3 mm,3 mm");//設(shè)置偏移邊框
TSCLIB_DLL.sendcommand("CLS");//清除記憶(每次打印新的條碼時(shí)先清除上一次的打印記憶)
}
//打印在用車檔案二維碼
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");
}
//打印財(cái)務(wù)條形碼
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");
}
}
//關(guān)閉打印機(jī)端口
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("請先選擇數(shù)據(jù)項(xiàng)!");
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#調(diào)用dll提示"試圖加載格式不正確的程序"解決方法
程序在32位操作系統(tǒng)上運(yùn)行正常,在64位操作系統(tǒng)上運(yùn)行讀卡功能提示”試圖加載格式不正確“。
程序在64位操作系統(tǒng)上運(yùn)行正常,在64位操作系統(tǒng)上運(yùn)行讀卡功能提示”試圖加載格式不正確“。
--------------------------------------------------------------------------------------------
點(diǎn)擊項(xiàng)目屬性,把目標(biāo)平臺Any CPU 設(shè)置為X86(32位操作系統(tǒng))或者X64(64位操作系統(tǒng))

按照上面解決方案可能會(huì)有下面的問題:

C# form繼承問題 : 提示 無法加載基類 ;請確保已引用該程序集并已生成所有項(xiàng)目。
把 生成 - 目標(biāo)平臺 改成 x86或者Any CPU ,重新生成一次,F(xiàn)orm可正常編輯。
所以新的解決方案是:選擇Any CPU 把下面打勾的去掉

以上這篇C# TSC打印二維碼和條形碼的實(shí)現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)體對象序列化成Json并讓字段的首字母小寫的兩種解決方法
這篇文章主要介紹了C#實(shí)體對象序列化成Json并讓字段的首字母小寫的兩種方法,在這兩種方法中小編比較推薦使用第二種方法,需要的朋友可以參考下2018-06-06
C#使用Jquery zTree實(shí)現(xiàn)樹狀結(jié)構(gòu)顯示 異步數(shù)據(jù)加載
這篇文章主要為大家詳細(xì)介紹了C#使用Jquery zTree實(shí)現(xiàn)樹狀結(jié)構(gòu)顯示和異步數(shù)據(jù)加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
詳解C#中Dictionary<TKey,TValue>的存儲(chǔ)結(jié)構(gòu)
無論是實(shí)際的項(xiàng)目中,還是在我們學(xué)習(xí)的過程中,都會(huì)重點(diǎn)的應(yīng)用到Dictionary<TKey,?TValue>這個(gè)存儲(chǔ)類型,所以本文就來為大家介紹一下這一存儲(chǔ)結(jié)構(gòu)的相關(guān)知識,希望對大家有所幫助2023-11-11
C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
C# 判斷時(shí)間段是否相交的實(shí)現(xiàn)方法
這篇文章主要介紹了C# 判斷時(shí)間段是否相交的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10
C#實(shí)現(xiàn)日期時(shí)間的格式化輸出的示例詳解
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)日期時(shí)間的格式化輸出的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-03-03
Unity報(bào)錯(cuò)InvalidOperationException: out of sync的解決
今天在做個(gè)東西,發(fā)現(xiàn)報(bào)錯(cuò),特此來記錄一下,本文介紹了Unity報(bào)錯(cuò)InvalidOperationException: out of sync的解決,感興趣的可以了解一下2021-05-05
c#實(shí)現(xiàn)sqlserver事務(wù)處理示例
這篇文章主要介紹了c#實(shí)現(xiàn)sqlserver事務(wù)處理的示例,大家參考使用吧2014-01-01
C# 使用Microsoft Edge WebView2的相關(guān)總結(jié)
這篇文章主要介紹了C# 使用Microsoft Edge WebView2的相關(guān)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02

