熱敏打印機通常提供兩種模式:
GS k)讓打印機內部生成二維碼,優點是速度快、不依賴圖片質量。GcPrintBitmap 打印位圖,優點是通用性強、支持 Logo 嵌入。本文聚焦 位圖模式,對應廠商 SDK 的 GcPrintBitmap。
int GcPrintBitmap(char *szBmpFile, int m);
| 參數 | 含義 | 典型值 |
|---|---|---|
| szBmpFile | BMP 文件完整路徑 | "C:\\temp\\qrcode.bmp" |
| m | 打印模式(垂直點數、密度、數據量) | 0,1,32,33 |
模式對照表:
m=0 8點單密度 67.7 DPI 101.6 DPI 數據量 = nL+nH×256
m=1 8點雙密度 67.7 DPI 203.2 DPI 數據量 = nL+nH×256
m=32 24點單密度 203.2 DPI 101.6 DPI 數據量 = (nL+nH×256)×3
m=33 24點雙密度 203.2 DPI 203.2 DPI 數據量 = (nL+nH×256)×3
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
public class ThermalPrinter {
[DllImport("Gprinter.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
private static extern int GcPrintBitmap(string bmpPath, int mode);
[DllImport("Gprinter.dll")]
private static extern int GcOpenPrinter(string printerName);
[DllImport("Gprinter.dll")]
private static extern int GcClosePrinter();
public bool PrintQR(string content, int mode = 33) {
// 1. 生成二維碼圖片 (需引用 QRCoder NuGet)
using (var qr = new QRCoder.QRCodeGenerator())
using (var data = qr.CreateQrCode(content, QRCoder.QRCodeGenerator.ECCLevel.Q))
using (var qrCode = new QRCoder.QRCode(data))
using (var bmp = qrCode.GetGraphic(4)) { // 4px per module
string path = Path.Combine(Path.GetTempPath(), "qr_" + Guid.NewGuid() + ".bmp");
bmp.Save(path, ImageFormat.Bmp);
// 2. 打開打印機 (根據實際驅動名稱)
if (GcOpenPrinter("GP-5890X") != 0) return false;
// 3. 打印位圖
int ret = GcPrintBitmap(path, mode);
// 4. 關閉並清理
GcClosePrinter();
File.Delete(path);
return ret == 0;
}
}
}
m=33 (24點雙密度) 適合 58mm/80mm 寬熱敏紙,清晰度最高。