Commit 31f50a5d 刘韬

添加TSC打印驱动

1 个父辈 1a83758c
此文件类型无法预览
此文件类型无法预览
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Printing;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
using System.Xml; using System.Xml;
...@@ -465,6 +466,8 @@ namespace BLL ...@@ -465,6 +466,8 @@ namespace BLL
if (labelIdx == -1) return; if (labelIdx == -1) return;
using (PrintDialog dlg = new PrintDialog()) using (PrintDialog dlg = new PrintDialog())
{ {
PrinterSettings printerSettings = new PrinterSettings();
dlg.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("C", 236, 157);
if (dlg.ShowDialog() != DialogResult.OK) return; if (dlg.ShowDialog() != DialogResult.OK) return;
System.Drawing.Printing.PrintDocument print = new System.Drawing.Printing.PrintDocument { System.Drawing.Printing.PrintDocument print = new System.Drawing.Printing.PrintDocument {
PrinterSettings = dlg.PrinterSettings PrinterSettings = dlg.PrinterSettings
...@@ -547,12 +550,19 @@ namespace BLL ...@@ -547,12 +550,19 @@ namespace BLL
print.PrinterSettings.PrinterName = PrinterName; print.PrinterSettings.PrinterName = PrinterName;
print.DefaultPageSettings.Landscape = PrintLandscape; print.DefaultPageSettings.Landscape = PrintLandscape;
print.PrintPage += PrintLast_PrintPage; print.PrintPage += PrintLast_PrintPage;
print.EndPrint += Print_EndPrint;
print.Print(); print.Print();
//print.Dispose(); //print.Dispose();
return bmp; return bmp;
} }
private void Print_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
//e.Cancel = true;
//throw new NotImplementedException();
}
private string StrRemoveKey(string s) private string StrRemoveKey(string s)
{ {
string rtn = ""; string rtn = "";
......
...@@ -3,7 +3,14 @@ using System; ...@@ -3,7 +3,14 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Management; using System.Management;
using System.Net.NetworkInformation;
using System.Printing; using System.Printing;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace Asa namespace Asa
{ {
...@@ -14,11 +21,12 @@ namespace Asa ...@@ -14,11 +21,12 @@ namespace Asa
private System.Threading.Thread tRun; private System.Threading.Thread tRun;
string dir;
public PrintLabel(string dir, int dpi = 100) public PrintLabel(string dir, int dpi = 100)
{ {
Common.MULTIPLE = dpi; Common.MULTIPLE = dpi;
Common.LabelPrint = new BLL.LabelPrint(); Common.LabelPrint = new BLL.LabelPrint();
this.dir = dir;
Common.LabelPrint.Load(dir); Common.LabelPrint.Load(dir);
} }
...@@ -68,6 +76,8 @@ namespace Asa ...@@ -68,6 +76,8 @@ namespace Asa
public void SetResolution(int printerDPI) public void SetResolution(int printerDPI)
{ {
Common.MULTIPLE = printerDPI; Common.MULTIPLE = printerDPI;
Common.LabelPrint = new BLL.LabelPrint();
Common.LabelPrint.Load(dir);
} }
public bool Rotate180 { set => Common.LabelPrint.Rotate180 = value; get => Common.LabelPrint.Rotate180; } public bool Rotate180 { set => Common.LabelPrint.Rotate180 = value; get => Common.LabelPrint.Rotate180; }
...@@ -234,8 +244,176 @@ namespace Asa ...@@ -234,8 +244,176 @@ namespace Asa
} }
} }
/// <summary>
/// 打印
/// </summary>
/// <param name="key"></param>
/// <returns>是否打印成功</returns>
public bool PrintToTsc(Dictionary<string, string> key,out TscStauts tscStauts)
{
tscStauts = CheckTscStatus();
if (tscStauts != 0) {
var err = (TscStauts)tscStauts;
Console.WriteLine($"Error: {err}");
return false;
}
var t = Task.Run(() =>
{
Bitmap bmp = null;
try
{
bmp = PrintPreview(key);
var mm = Common.PxToMM(bmp.Width);
var nn = Common.PxToMM(bmp.Height);
TSCLIB_DLL.openport("USB");
SendCommand("GETSENSOR(\"PEEL\")");
SendCommand($"SIZE {mm:0.00} mm, {nn:0.00} mm");
SendCommand("GAP 3 mm,0.3 mm");
SendCommand("SET CUTTER OFF");
SendCommand("SET RIBBON OFF"); //使用碳带
SendCommand("SET ENCODER OFF"); //碳带编码器检测
SendCommand("OFFSET -10 mm"); //出纸偏移
SendCommand("SPEED 9"); //速度最大15
SendCommand("DENSITY 12"); //浓度
SendCommand("DIRECTION 1");
SendCommand("SET PEEL OFF");
TSCLIB_DLL.clearbuffer();
TSCLIB_DLL.sendpicture(0, 0, bmp);
TSCLIB_DLL.printlabel("1", "1");
TSCLIB_DLL.closeport();
Task.Delay(350).Wait();
}
catch (Exception e) { }
finally {
bmp?.Dispose();
}
});
t.Wait(1000);
tscStauts = CheckTscStatus();
Console.WriteLine("After print:" + tscStauts);
if (tscStauts == TscStauts.打印中 || tscStauts == TscStauts.准备就绪 || tscStauts == TscStauts.None)
return true;
else {
return false;
}
//Console.WriteLine("usbportqueryprinter:" + TSCLIB_DLL.usbportqueryprinter());
}
void SendCommand(string command)
{
Console.WriteLine(TSCLIB_DLL.sendcommand(command));
}
/// <summary>
/// 打印
/// </summary>
/// <param name="key"></param>
/// <returns>是否打印成功</returns>
public bool PrintToTsc1(Dictionary<string, string> key, out TscStauts tscStauts)
{
tscStauts = CheckTscStatus();
if (tscStauts != 0)
{
var err = (TscStauts)tscStauts;
Console.WriteLine($"Error: {err}");
return false;
}
var t = Task.Run(() =>
{
Bitmap bmp = null;
try
{
bmp = PrintPreview(key);
var mm = Common.PxToMM(bmp.Width);
var nn = Common.PxToMM(bmp.Height);
TSCSDK.driver TSCLIB_DLL = new TSCSDK.driver();
TSCLIB_DLL.openport("TSC TTP-342 Pro");
//TSCLIB_DLL.sendcommand("GETSENSOR(\"PEEL\")");
TSCLIB_DLL.sendcommand("\0x27!?");
Task.Delay(1000).Wait();
///IntPtr aaa = Marshal.AllocHGlobal(256);
byte[] b = new byte[256];
IntPtr aaa = System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(b, 0);
TSCSDK.driver.ReadPrinter(TSCLIB_DLL.hPrinter, aaa, 255, out int reby);
//Marshal.Copy(aaa, b, 0, 256);
//Marshal.FreeHGlobal(aaa);
var bbbb = Encoding.Unicode.GetString(b);
TSCLIB_DLL.sendcommand($"SIZE {mm:0.00} mm, {nn:0.00} mm");
TSCLIB_DLL.sendcommand("GAP 3 mm,0.3 mm");
TSCLIB_DLL.sendcommand("SET CUTTER OFF");
TSCLIB_DLL.sendcommand("SET RIBBON OFF"); //使用碳带
TSCLIB_DLL.sendcommand("SET ENCODER OFF"); //碳带编码器检测
TSCLIB_DLL.sendcommand("OFFSET -10 mm"); //出纸偏移
TSCLIB_DLL.sendcommand("SPEED 9"); //速度最大15
TSCLIB_DLL.sendcommand("DENSITY 12"); //浓度
TSCLIB_DLL.sendcommand("DIRECTION 1");
TSCLIB_DLL.sendcommand("SET PEEL OFF");
TSCLIB_DLL.clearbuffer();
TSCLIB_DLL.sendpicture(0, 0, bmp);
TSCLIB_DLL.printlabel("1", "1");
TSCLIB_DLL.closeport();
Task.Delay(350).Wait();
}
catch (Exception e) { }
finally
{
bmp?.Dispose();
}
});
t.Wait(1000);
tscStauts = CheckTscStatus();
Console.WriteLine("After print:" + tscStauts);
if (tscStauts == TscStauts.打印中 || tscStauts == TscStauts.准备就绪 || tscStauts == TscStauts.None)
return true;
else
{
return false;
}
//Console.WriteLine("usbportqueryprinter:" + TSCLIB_DLL.usbportqueryprinter());
}
TscStauts CheckTscStatus() {
TscStauts s = TscStauts.None;
var t = Task.Run(() => {
Console.WriteLine("TID:"+ GetCurrentThreadId().ToString("X"));
s = (TscStauts)TSCLIB_DLL.usbportqueryprinter();
});
if (!t.Wait(1000))
Console.WriteLine("usbportqueryprinter time out");
return s;
}
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();
}
public enum TscStauts
{
准备就绪 = 0x0,
打印头开启 = 0x1,
纸张卡纸 = 0x02,
打印头开启并且纸张卡纸 = 0x03,
纸张缺纸 = 0x04,
打印头开启并且纸张缺纸 = 0x05,
无碳带 = 0x08,
打印头开启并且无碳带 = 0x09,
纸张卡纸并且无碳带 = 0x0A,
打印头开启_纸张卡纸并且无碳带 = 0x0B,
纸张缺纸并且无碳带 = 0x0C,
打印头开启_纸张缺纸并且无碳带 = 0x0D,
暂停 = 0x10,
Error = 0x14,
打印中 = 0x20,
其它错误 = 0x80,
None = 0xFD,
Offline = 0xFF,
}
enum TscStautsaa {
a = 1,
b = 1<<1,
c = 1<<2,
} }
} }
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
...@@ -30,6 +31,7 @@ ...@@ -30,6 +31,7 @@
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Microsoft.VisualBasic" /> <Reference Include="Microsoft.VisualBasic" />
...@@ -71,6 +73,8 @@ ...@@ -71,6 +73,8 @@
</Compile> </Compile>
<Compile Include="PrintLabel.cs" /> <Compile Include="PrintLabel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TSCLIB_DLL.cs" />
<Compile Include="TSCSDK.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="FormTextBox.resx"> <EmbeddedResource Include="FormTextBox.resx">
......
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Asa
{
public class TSCLIB_DLL
{
[DllImport("TSCLIB.dll", EntryPoint = "about")]
public static extern int about();
/// <summary>
/// <list>說明: 指定電腦端的輸出埠</list>
/// 參數:
/// (1) 單機列印時,請指定印表機驅動程式名稱
/// 例如: TSC CLEVER TTP-243
/// (2) 若連接印表機伺服器,請指定伺服器路徑及共用印表機名稱
/// 例如: \\SERVER\TTP243
/// (3) 直接指定平行傳輸介面,請指定輸出埠名稱為 LPT1 到 LPT4
/// (4) 直接指定 USB 傳輸介面,請指定輸出埠名稱為 USB
/// </summary>
/// <param name="printername"></param>
/// <returns></returns>
[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);
[DllImport("TSCLIB.dll", EntryPoint = "windowsfontUnicode")]
public static extern int windowsfontUnicode(int x, int y, int fontheight,
int rotation, int fontstyle, int fontunderline,
string szFaceName, byte[] content);
[DllImport("TSCLIB.dll", EntryPoint = "sendBinaryData")]
public static extern int sendBinaryData(byte[] content, int length);
[DllImport("TSCLIB.dll", EntryPoint = "usbportqueryprinter")]
public static extern byte usbportqueryprinter();
[DllImport("TSCLIB.dll", EntryPoint = "usbprinterstatus")]
public static extern byte usbprinterstatus();
[DllImport("TSCLIB.dll", EntryPoint = "usbprinterpeel")]
public static extern int usbprinterpeel(string printercommand);
public static void sendpicture(int xpoint, int ypoint, Bitmap original_picture)
{
//Bitmap bitmap = new Bitmap(original_picture.Width, original_picture.Height);
//for (int i = 0; i < original_picture.Width; i++)
//{
// for (int j = 0; j < original_picture.Height; j++)
// {
// Color pixel = original_picture.GetPixel(i, j);
// int num = (int)((double)(int)pixel.R * 0.3 + (double)(int)pixel.G * 0.59 + (double)(int)pixel.B * 0.11);
// bitmap.SetPixel(i, j, Color.FromArgb(pixel.A, num, num, num));
// }
//}
Bitmap bitmap2 = original_picture;
new Bitmap(bitmap2.Width, bitmap2.Height);
byte[] result_utf8 = System.Text.Encoding.UTF8.GetBytes("BITMAP " + xpoint + "," + ypoint + "," + (bitmap2.Width + 7) / 8 + "," + bitmap2.Height + ", 0,");
sendBinaryData(result_utf8, result_utf8.Length);
byte[] array = new byte[(bitmap2.Width + 7) / 8 * bitmap2.Height];
for (int k = 0; k < array.Length; k++)
{
array[k] = byte.MaxValue;
}
for (int l = 0; l < bitmap2.Height; l++)
{
for (int m = 0; m < bitmap2.Width; m++)
{
Color pixel2 = bitmap2.GetPixel(m, l);
byte r = pixel2.R;
byte g = pixel2.G;
byte b = pixel2.B;
if ((r + g + b) / 3 < 128)
{
array[l * ((bitmap2.Width + 7) / 8) + m / 8] ^= (byte)(128 >> m % 8);
}
}
}
sendBinaryData(array, array.Length);
sendBinaryData(CRLF_byte, CRLF_byte.Length);
}
private static byte[] CRLF_byte = new byte[2] { 13, 10 };
}
}

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Globalization;
using System.IO;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace TSCSDK
{
public class driver
{
[StructLayout(LayoutKind.Sequential)]
private class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class LOGFONT
{
public const int LF_FACESIZE = 32;
public int lfHeight;
public int lfWidth;
public int lfEscapement;
public int lfOrientation;
public int lfWeight;
public byte lfItalic;
public byte lfUnderline;
public byte lfStrikeOut;
public byte lfCharSet;
public byte lfOutPrecision;
public byte lfClipPrecision;
public byte lfQuality;
public byte lfPitchAndFamily;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string lfFaceName;
}
public struct SIZE
{
public int cx;
public int cy;
public SIZE(int cx, int cy)
{
this.cx = cx;
this.cy = cy;
}
}
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public int X
{
get
{
return Left;
}
set
{
Right -= Left - value;
Left = value;
}
}
public int Y
{
get
{
return Top;
}
set
{
Bottom -= Top - value;
Top = value;
}
}
public int Height
{
get
{
return Bottom - Top;
}
set
{
Bottom = value + Top;
}
}
public int Width
{
get
{
return Right - Left;
}
set
{
Right = value + Left;
}
}
public Point Location
{
get
{
return new Point(Left, Top);
}
set
{
X = value.X;
Y = value.Y;
}
}
public Size Size
{
get
{
return new Size(Width, Height);
}
set
{
Width = value.Width;
Height = value.Height;
}
}
public RECT(int left, int top, int right, int bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
public RECT(Rectangle r)
: this(r.Left, r.Top, r.Right, r.Bottom)
{
}
public static implicit operator Rectangle(RECT r)
{
return new Rectangle(r.Left, r.Top, r.Width, r.Height);
}
public static implicit operator RECT(Rectangle r)
{
return new RECT(r);
}
public static bool operator ==(RECT r1, RECT r2)
{
return r1.Equals(r2);
}
public static bool operator !=(RECT r1, RECT r2)
{
return !r1.Equals(r2);
}
public bool Equals(RECT r)
{
if (r.Left == Left && r.Top == Top && r.Right == Right)
{
return r.Bottom == Bottom;
}
return false;
}
public override bool Equals(object obj)
{
if (obj is RECT)
{
return Equals((RECT)obj);
}
if (obj is Rectangle)
{
return Equals(new RECT((Rectangle)obj));
}
return false;
}
public override int GetHashCode()
{
return ((Rectangle)this).GetHashCode();
}
public override string ToString()
{
return string.Format(CultureInfo.CurrentCulture, "{{Left={0},Top={1},Right={2},Bottom={3}}}", Left, Top, Right, Bottom);
}
}
public IntPtr hPrinter;
private static string CRLF = "\r\n";
private static byte[] CRLF_byte = new byte[2] { 13, 10 };
private int iTop = 0;
private int iBitmapWidth;
private int iBitmapHeight;
private int iBitmapX;
private int iBitmapY;
private int TextOut_X_start;
private int TextOut_Y_start;
private byte[] buf = new byte[5760000];
private int imgShiftX = 0;
private const int OUT_DEFAULT_PRECIS = 0;
private const int CLIP_DEFAULT_PRECIS = 0;
private const int BUFFER_WIDTH = 2400;
private const int BUFFER_HEIGHT = 2400;
private int dwCount;
private int CRLFCount;
private int dwWritten = 0;
private IntPtr pBytes;
private IntPtr CRLFBytes;
[DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "OpenPrinterA", ExactSpelling = true, SetLastError = true)]
private static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
private static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "StartDocPrinterA", ExactSpelling = true, SetLastError = true)]
private static extern bool StartDocPrinter(IntPtr hPrinter, int level, [In][MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
private static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
private static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
private static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
private static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, int dwCount, out int dwWritten);
[DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
private static extern bool WritePrinter(IntPtr hPrinter, byte[] Bytes, int dwCount, out int dwWritten);
[DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
public static extern bool ReadPrinter(IntPtr hPrinter, IntPtr pBuf, int cbBuf, out int pNoBytesRead);
[DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
public static extern bool ReadPrinter(IntPtr hPrinter, byte[] Bytes, int cbBuf, out int pNoBytesRead);
[DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
public static extern bool ReadPrinter(IntPtr hPrinter, out StringBuilder data, int cbBuf, out int pNoBytesRead);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr CreateFontIndirect([In][MarshalAs(UnmanagedType.LPStruct)] LOGFONT lplf);
[DllImport("gdi32.dll", SetLastError = true)]
private static extern IntPtr CreateCompatibleDC([In] IntPtr hdc);
[DllImport("gdi32.dll")]
private static extern IntPtr CreateBitmap(int nWidth, int nHeight, uint cPlanes, uint cBitsPerPel, IntPtr lpvBits);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject([In] IntPtr hdc, [In] IntPtr hgdiobj);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll")]
private static extern uint SetTextColor(IntPtr hdc, int crColor);
[DllImport("gdi32.dll")]
private static extern uint SetBkColor(IntPtr hdc, int crColor);
[DllImport("gdi32.dll")]
private static extern bool Rectangle(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
[DllImport("user32.dll")]
private static extern int FillRect(IntPtr hDC, [In] ref RECT lprc, IntPtr hbr);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
private static extern bool TextOut(IntPtr hdc, int nXStart, int nYStart, string lpString, int cbString);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
private static extern bool TextOutW(IntPtr hdc, int nXStart, int nYStart, string lpWString, int cbString);
[DllImport("gdi32.dll")]
private static extern int GetBitmapBits(IntPtr hbmp, int cbBuffer, [Out] byte[] lpvBits);
[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC([In] IntPtr hdc);
[DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
private static extern bool GetTextExtentPoint32(IntPtr hdc, string lpString, int cbString, out SIZE lpSize);
[DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
private static extern bool GetTextExtentPoint32W(IntPtr hdc, string lpWString, int cbString, out SIZE lpSize);
public bool openport(string szPrinterName)
{
hPrinter = new IntPtr(0);
DOCINFOA dOCINFOA = new DOCINFOA();
bool flag = false;
dOCINFOA.pDocName = "My C#.NET RAW Document";
dOCINFOA.pDataType = "RAW";
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero) && StartDocPrinter(hPrinter, 1, dOCINFOA) && StartPagePrinter(hPrinter))
{
return true;
}
if (!flag)
{
Marshal.GetLastWin32Error();
}
return false;
}
public bool driver_status(string printerName)
{
using (ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher($"SELECT * from Win32_Printer WHERE Name LIKE '%{printerName}'"))
{
using (ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get())
{
try
{
foreach (ManagementObject item in managementObjectCollection)
{
foreach (PropertyData property in item.Properties)
{
if (property.Name == "WorkOffline")
{
if (property.Value.ToString() == "False")
{
return true;
}
return false;
}
}
}
}
catch (ManagementException)
{
return false;
}
}
}
return false;
}
public string show_install_driver()
{
string text = "";
for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
{
string text2 = PrinterSettings.InstalledPrinters[i];
text = text + text2 + ";";
}
return text;
}
public bool sendcommand(string command)
{
dwCount = command.Length;
CRLFCount = CRLF.Length;
pBytes = Marshal.StringToCoTaskMemAnsi(command);
CRLFBytes = Marshal.StringToCoTaskMemAnsi(CRLF);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
WritePrinter(hPrinter, CRLFBytes, CRLFCount, out dwWritten);
return true;
}
Marshal.FreeCoTaskMem(pBytes);
return false;
}
public void sendcommand(string[] command)
{
for (int i = 0; i < command.Length; i++)
{
if (command[i] != "")
{
byte[] bytes = Encoding.Default.GetBytes(command[i]);
sendcommand(bytes);
sendcommand(CRLF_byte);
}
}
}
public int sendcommand_hex(string hex_data)
{
string text = "";
string text2 = "";
int num = 0;
int num2 = 0;
for (int i = 0; i < hex_data.Length; i++)
{
text += hex_data.Substring(i, 1);
num++;
if (num == 2)
{
char c = Convert.ToChar(Convert.ToUInt32(text.Substring(num2, 2), 16));
text2 += $"{c:X}";
num = 0;
num2 += 2;
}
}
byte[] bytes = Encoding.ASCII.GetBytes(text2);
sendcommand(bytes);
return 1;
}
public int sendcommand_hex(string[] hex_data)
{
string text = "";
for (int i = 0; i < hex_data.Length; i++)
{
char c = Convert.ToChar(Convert.ToUInt32(hex_data[i].Substring(0, 2), 16));
text += $"{c:X}";
}
Encoding.ASCII.GetBytes(text);
return 1;
}
public int sendASCtoHEX(string hexString)
{
byte[] array = new byte[hexString.Length / 2];
for (int i = 0; i < hexString.Length; i += 2)
{
array[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
}
sendcommand(array);
return 1;
}
public bool sendcommand_utf8(string command)
{
byte[] bytes = Encoding.UTF8.GetBytes(command);
dwCount = bytes.Length;
CRLFCount = CRLF.Length;
CRLFBytes = Marshal.StringToCoTaskMemAnsi(CRLF);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, dwCount, out dwWritten);
WritePrinter(hPrinter, CRLFBytes, CRLFCount, out dwWritten);
return true;
}
Marshal.FreeCoTaskMem(CRLFBytes);
return false;
}
public bool sendcommand_gb2312(string command)
{
byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes(command);
dwCount = bytes.Length;
CRLFCount = CRLF.Length;
CRLFBytes = Marshal.StringToCoTaskMemAnsi(CRLF);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, dwCount, out dwWritten);
WritePrinter(hPrinter, CRLFBytes, CRLFCount, out dwWritten);
return true;
}
Marshal.FreeCoTaskMem(CRLFBytes);
return false;
}
public bool sendcommand_big5(string command)
{
byte[] bytes = Encoding.GetEncoding("big5").GetBytes(command);
dwCount = bytes.Length;
CRLFCount = CRLF.Length;
CRLFBytes = Marshal.StringToCoTaskMemAnsi(CRLF);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, dwCount, out dwWritten);
WritePrinter(hPrinter, CRLFBytes, CRLFCount, out dwWritten);
return true;
}
Marshal.FreeCoTaskMem(CRLFBytes);
return false;
}
public int sendcommandNOCRLF(string command)
{
dwCount = command.Length;
CRLFCount = CRLF.Length;
pBytes = Marshal.StringToCoTaskMemAnsi(command);
CRLFBytes = Marshal.StringToCoTaskMemAnsi(CRLF);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
return 1;
}
Marshal.FreeCoTaskMem(pBytes);
return -1;
}
public bool sendbinary(byte[] command)
{
dwCount = command.Length;
CRLFCount = CRLF.Length;
CRLFBytes = Marshal.StringToCoTaskMemAnsi(CRLF);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, command, dwCount, out dwWritten);
WritePrinter(hPrinter, CRLFBytes, CRLFCount, out dwWritten);
return true;
}
return false;
}
public bool sendcommand(byte[] command)
{
dwCount = command.Length;
CRLFCount = CRLF.Length;
CRLFBytes = Marshal.StringToCoTaskMemAnsi(CRLF);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, command, dwCount, out dwWritten);
WritePrinter(hPrinter, CRLFBytes, CRLFCount, out dwWritten);
return true;
}
return false;
}
public bool sendcommandNOCRLF(byte[] command)
{
dwCount = command.Length;
CRLFCount = CRLF.Length;
CRLFBytes = Marshal.StringToCoTaskMemAnsi(CRLF);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, command, dwCount, out dwWritten);
return true;
}
return false;
}
public bool closeport()
{
EndDocPrinter(hPrinter);
EndPagePrinter(hPrinter);
ClosePrinter(hPrinter);
return true;
}
public void setup(string width, string height, string speed, string density, string sensor, string vertical, string offset)
{
string s = "SIZE " + width + " mm," + height + " mm\r\n";
string s2 = "SPEED " + speed + "\r\n";
string s3 = "DENSITY " + density + "\r\n";
string s4 = "";
if (sensor == "0")
{
s4 = "GAP " + vertical + " mm, " + offset + " mm\r\n";
}
else if (sensor == "1")
{
s4 = "BLINE " + vertical + " mm, " + offset + " mm\r\n";
}
byte[] bytes = Encoding.ASCII.GetBytes(s);
byte[] bytes2 = Encoding.ASCII.GetBytes(s2);
byte[] bytes3 = Encoding.ASCII.GetBytes(s3);
byte[] bytes4 = Encoding.ASCII.GetBytes(s4);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, bytes.Length, out dwWritten);
WritePrinter(hPrinter, bytes2, bytes2.Length, out dwWritten);
WritePrinter(hPrinter, bytes3, bytes3.Length, out dwWritten);
WritePrinter(hPrinter, bytes4, bytes4.Length, out dwWritten);
}
}
public void clearbuffer()
{
string s = "CLS\r\n";
byte[] bytes = Encoding.ASCII.GetBytes(s);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, bytes.Length, out dwWritten);
}
}
public void barcode(string x, string y, string type, string height, string readable, string rotation, string narrow, string wide, string code)
{
string s = "BARCODE " + x + "," + y + ",\"" + type + "\"," + height + "," + readable + "," + rotation + "," + narrow + "," + wide + ",\"" + code + "\"\r\n";
byte[] bytes = Encoding.ASCII.GetBytes(s);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, bytes.Length, out dwWritten);
}
}
public void printerfont(string x, string y, string fonttype, string rotation, string xmul, string ymul, string text)
{
string s = "TEXT " + x + "," + y + ",\"" + fonttype + "\"," + rotation + "," + xmul + "," + ymul + ",\"" + text + "\"\r\n";
byte[] bytes = Encoding.ASCII.GetBytes(s);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, bytes.Length, out dwWritten);
}
}
public void printlabel(string a, string b)
{
string s = "PRINT " + a + ", " + b + "\r\n";
byte[] bytes = Encoding.ASCII.GetBytes(s);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, bytes.Length, out dwWritten);
}
}
public void formfeed()
{
string s = "FORMFEED\r\n";
byte[] bytes = Encoding.ASCII.GetBytes(s);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, bytes.Length, out dwWritten);
}
}
public void nobackfeed()
{
string s = "SET TEAR OFF\r\n";
byte[] bytes = Encoding.ASCII.GetBytes(s);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, bytes.Length, out dwWritten);
}
}
public int downloadpcx(string filename, string imagename)
{
byte[] array = File.ReadAllBytes(filename);
long num = array.Length;
string s = "DOWNLOAD F,\"" + imagename + "\"," + num + ",";
byte[] bytes = Encoding.ASCII.GetBytes(s);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, bytes.Length, out dwWritten);
WritePrinter(hPrinter, array, array.Length, out dwWritten);
WritePrinter(hPrinter, CRLF_byte, CRLF_byte.Length, out dwWritten);
return 1;
}
return 0;
}
public int downloadbmp(string filename, string imagename)
{
byte[] array = File.ReadAllBytes(filename);
long num = array.Length;
string s = "DOWNLOAD F,\"" + imagename + "\"," + num + ",";
byte[] bytes = Encoding.ASCII.GetBytes(s);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, bytes.Length, out dwWritten);
WritePrinter(hPrinter, array, array.Length, out dwWritten);
WritePrinter(hPrinter, CRLF_byte, CRLF_byte.Length, out dwWritten);
return 1;
}
return 0;
}
public int downloadfile(string filename, string downloadname)
{
byte[] array = File.ReadAllBytes(filename);
long num = array.Length;
_ = "DOWNLOAD F,\"" + downloadname + "\"," + num + ",";
byte[] bytes = Encoding.ASCII.GetBytes(downloadname);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, bytes.Length, out dwWritten);
WritePrinter(hPrinter, array, array.Length, out dwWritten);
WritePrinter(hPrinter, CRLF_byte, CRLF_byte.Length, out dwWritten);
return 1;
}
return 0;
}
public int downloadfile(string filename, string location, string downloadname)
{
byte[] array = File.ReadAllBytes(filename);
long num = array.Length;
_ = "DOWNLOAD " + location + ",\"" + downloadname + "\"," + num + ",";
byte[] bytes = Encoding.ASCII.GetBytes(downloadname);
if (StartPagePrinter(hPrinter))
{
WritePrinter(hPrinter, bytes, bytes.Length, out dwWritten);
WritePrinter(hPrinter, array, array.Length, out dwWritten);
WritePrinter(hPrinter, CRLF_byte, CRLF_byte.Length, out dwWritten);
return 1;
}
return 0;
}
public void about()
{
MessageBox.Show("This is .NET SDK V1.0A");
}
public void printerrestart()
{
byte[] command = new byte[3] { 27, 33, 82 };
sendcommand(command);
}
public void windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, string szFaceName, string content)
{
LOGFONT lOGFONT = new LOGFONT();
SIZE lpSize = default(SIZE);
lOGFONT.lfWidth = 0;
lOGFONT.lfEscapement = 0;
lOGFONT.lfOrientation = 0;
lOGFONT.lfCharSet = 1;
lOGFONT.lfOutPrecision = 0;
lOGFONT.lfClipPrecision = 0;
lOGFONT.lfQuality = 1;
lOGFONT.lfPitchAndFamily = 26;
lOGFONT.lfFaceName = szFaceName;
lOGFONT.lfHeight = fontheight;
lOGFONT.lfItalic = 0;
lOGFONT.lfUnderline = 0;
lOGFONT.lfStrikeOut = 0;
lOGFONT.lfWeight = ((fontstyle < 2) ? 400 : 700);
lOGFONT.lfEscapement = rotation * 10;
IntPtr dC = GetDC(IntPtr.Zero);
IntPtr intPtr = CreateCompatibleDC(dC);
IntPtr intPtr2 = CreateBitmap(2400, 2400, 1u, 1u, IntPtr.Zero);
SelectObject(intPtr, intPtr2);
IntPtr hgdiobj = CreateFontIndirect(lOGFONT);
hgdiobj = SelectObject(intPtr, hgdiobj);
GetTextExtentPoint32(intPtr, content, content.Length, out lpSize);
SetTextColor(intPtr, ColorTranslator.ToWin32(Color.Black));
SetBkColor(intPtr, ColorTranslator.ToWin32(Color.White));
iBitmapWidth = ((rotation == 0 || rotation == 180) ? ((lpSize.cx + 7) / 8) : ((lpSize.cy + 7) / 8));
iBitmapHeight = ((rotation == 90 || rotation == 270) ? lpSize.cx : lpSize.cy);
RECT lprc = default(RECT);
lprc.Left = 0;
lprc.Top = 0;
lprc.Right = ((rotation == 0 || rotation == 180) ? (lpSize.cx + 16) : (lpSize.cy + 16));
lprc.Bottom = ((rotation == 90 || rotation == 270) ? (lpSize.cx + 16) : (lpSize.cy + 16));
FillRect(intPtr, ref lprc, IntPtr.Zero);
int textOut_X_start;
switch (rotation)
{
default:
textOut_X_start = lpSize.cy;
break;
case 180:
textOut_X_start = lpSize.cx;
break;
case 0:
case 90:
textOut_X_start = 0;
break;
}
TextOut_X_start = textOut_X_start;
TextOut_Y_start = ((rotation != 0 && rotation != 270) ? iBitmapHeight : 0);
TextOut(intPtr, TextOut_X_start, TextOut_Y_start, content, content.Length);
GetBitmapBits(intPtr2, 5760000, buf);
if (!DeleteObject(SelectObject(intPtr, hgdiobj)))
{
MessageBox.Show("Select hFont=0", "title");
}
if (!DeleteDC(intPtr))
{
MessageBox.Show("hdcMem=0", "title");
}
if (!DeleteObject(intPtr2))
{
MessageBox.Show("hBitmap=0", "title");
}
int num;
switch (rotation)
{
default:
num = x - lpSize.cy;
break;
case 180:
num = x - lpSize.cx;
break;
case 0:
case 90:
num = x;
break;
}
iBitmapX = num;
iBitmapY = ((rotation == 0 || rotation == 270) ? y : (y - iBitmapHeight));
if (iBitmapY < 0)
{
iTop -= iBitmapY;
iBitmapY = 0;
}
if (iBitmapX < 0)
{
imgShiftX -= (iBitmapX - 7) / 8;
iBitmapX = 0;
}
string text = "BITMAP " + iBitmapX + "," + iBitmapY + "," + (iBitmapWidth - imgShiftX) + "," + (iBitmapHeight - iTop) + ",1,";
Encoding.UTF8.GetBytes(text);
dwCount = text.Length;
CRLFCount = CRLF.Length;
pBytes = Marshal.StringToCoTaskMemAnsi(text);
CRLFBytes = Marshal.StringToCoTaskMemAnsi(CRLF);
WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
GC.Collect();
Encoding.Unicode.GetChars(buf);
for (int i = iTop; i < iBitmapHeight; i++)
{
int num2 = imgShiftX;
while (num2 < iBitmapWidth)
{
byte[] array = new byte[300];
Marshal.SizeOf((object)array[0]);
_ = array.Length;
IntPtr intPtr3 = Marshal.AllocHGlobal(5760000);
Marshal.Copy(buf, i * 300, intPtr3, 5760000 - i * 300);
byte[] array2 = new byte[300];
Marshal.Copy(intPtr3, array2, 0, 300);
WritePrinter(hPrinter, array2, iBitmapWidth, out dwWritten);
num2 += iBitmapWidth;
array2 = null;
Marshal.FreeHGlobal(intPtr3);
GC.Collect();
}
}
WritePrinter(hPrinter, CRLFBytes, CRLFCount, out dwWritten);
Marshal.Release(intPtr2);
Marshal.Release(intPtr);
Marshal.Release(dC);
Marshal.Release(pBytes);
Marshal.Release(CRLFBytes);
GC.Collect();
}
public void windowsfontunicode(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, string szFaceName, string content)
{
LOGFONT lOGFONT = new LOGFONT();
SIZE lpSize = default(SIZE);
lOGFONT.lfWidth = 0;
lOGFONT.lfEscapement = 0;
lOGFONT.lfOrientation = 0;
lOGFONT.lfCharSet = 1;
lOGFONT.lfOutPrecision = 0;
lOGFONT.lfClipPrecision = 0;
lOGFONT.lfQuality = 1;
lOGFONT.lfPitchAndFamily = 26;
lOGFONT.lfFaceName = szFaceName;
lOGFONT.lfHeight = fontheight;
lOGFONT.lfItalic = 0;
lOGFONT.lfUnderline = 0;
lOGFONT.lfStrikeOut = 0;
lOGFONT.lfWeight = ((fontstyle < 2) ? 400 : 700);
lOGFONT.lfEscapement = rotation * 10;
IntPtr dC = GetDC(IntPtr.Zero);
IntPtr intPtr = CreateCompatibleDC(dC);
IntPtr intPtr2 = CreateBitmap(2400, 2400, 1u, 1u, IntPtr.Zero);
SelectObject(intPtr, intPtr2);
IntPtr hgdiobj = CreateFontIndirect(lOGFONT);
hgdiobj = SelectObject(intPtr, hgdiobj);
GetTextExtentPoint32W(intPtr, content, content.Length, out lpSize);
SetTextColor(intPtr, ColorTranslator.ToWin32(Color.Black));
SetBkColor(intPtr, ColorTranslator.ToWin32(Color.White));
iBitmapWidth = ((rotation == 0 || rotation == 180) ? ((lpSize.cx + 7) / 8) : ((lpSize.cy + 7) / 8));
iBitmapHeight = ((rotation == 90 || rotation == 270) ? lpSize.cx : lpSize.cy);
RECT lprc = default(RECT);
lprc.Left = 0;
lprc.Top = 0;
lprc.Right = ((rotation == 0 || rotation == 180) ? (lpSize.cx + 16) : (lpSize.cy + 16));
lprc.Bottom = ((rotation == 90 || rotation == 270) ? (lpSize.cx + 16) : (lpSize.cy + 16));
FillRect(intPtr, ref lprc, IntPtr.Zero);
int textOut_X_start;
switch (rotation)
{
default:
textOut_X_start = lpSize.cy;
break;
case 180:
textOut_X_start = lpSize.cx;
break;
case 0:
case 90:
textOut_X_start = 0;
break;
}
TextOut_X_start = textOut_X_start;
TextOut_Y_start = ((rotation != 0 && rotation != 270) ? iBitmapHeight : 0);
TextOutW(intPtr, TextOut_X_start, TextOut_Y_start, content, content.Length);
GetBitmapBits(intPtr2, 5760000, buf);
if (!DeleteObject(SelectObject(intPtr, hgdiobj)))
{
MessageBox.Show("Select hFont=0", "title");
}
if (!DeleteDC(intPtr))
{
MessageBox.Show("hdcMem=0", "title");
}
if (!DeleteObject(intPtr2))
{
MessageBox.Show("hBitmap=0", "title");
}
int num;
switch (rotation)
{
default:
num = x - lpSize.cy;
break;
case 180:
num = x - lpSize.cx;
break;
case 0:
case 90:
num = x;
break;
}
iBitmapX = num;
iBitmapY = ((rotation == 0 || rotation == 270) ? y : (y - iBitmapHeight));
if (iBitmapY < 0)
{
iTop -= iBitmapY;
iBitmapY = 0;
}
if (iBitmapX < 0)
{
imgShiftX -= (iBitmapX - 7) / 8;
iBitmapX = 0;
}
string text = "BITMAP " + iBitmapX + "," + iBitmapY + "," + (iBitmapWidth - imgShiftX) + "," + (iBitmapHeight - iTop) + ",1,";
Encoding.UTF8.GetBytes(text);
dwCount = text.Length;
CRLFCount = CRLF.Length;
pBytes = Marshal.StringToCoTaskMemAnsi(text);
CRLFBytes = Marshal.StringToCoTaskMemAnsi(CRLF);
WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
GC.Collect();
Encoding.Unicode.GetChars(buf);
for (int i = iTop; i < iBitmapHeight; i++)
{
int num2 = imgShiftX;
while (num2 < iBitmapWidth)
{
byte[] array = new byte[300];
Marshal.SizeOf((object)array[0]);
_ = array.Length;
IntPtr intPtr3 = Marshal.AllocHGlobal(5760000);
Marshal.Copy(buf, i * 300, intPtr3, 5760000 - i * 300);
byte[] array2 = new byte[300];
Marshal.Copy(intPtr3, array2, 0, 300);
WritePrinter(hPrinter, array2, iBitmapWidth, out dwWritten);
num2 += iBitmapWidth;
array2 = null;
Marshal.FreeHGlobal(intPtr3);
GC.Collect();
}
}
WritePrinter(hPrinter, CRLFBytes, CRLFCount, out dwWritten);
Marshal.Release(intPtr2);
Marshal.Release(intPtr);
Marshal.Release(dC);
Marshal.Release(pBytes);
Marshal.Release(CRLFBytes);
GC.Collect();
}
public void printphoto(int xpoint, int ypoint, string filename)
{
Bitmap bitmap = new Bitmap(filename);
Bitmap bitmap2 = new Bitmap(bitmap.Width, bitmap.Height);
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
Color pixel = bitmap.GetPixel(i, j);
int num = (int)((double)(int)pixel.R * 0.3 + (double)(int)pixel.G * 0.59 + (double)(int)pixel.B * 0.11);
bitmap2.SetPixel(i, j, Color.FromArgb(pixel.A, num, num, num));
}
}
Bitmap bitmap3 = bitmap2;
new Bitmap(bitmap3.Width, bitmap3.Height);
sendcommandNOCRLF("BITMAP " + xpoint + "," + ypoint + "," + (bitmap3.Width + 7) / 8 + "," + bitmap3.Height + ", 0,");
byte[] array = new byte[(bitmap3.Width + 7) / 8 * bitmap3.Height];
for (int k = 0; k < array.Length; k++)
{
array[k] = byte.MaxValue;
}
for (int l = 0; l < bitmap3.Height; l++)
{
for (int m = 0; m < bitmap3.Width; m++)
{
Color pixel2 = bitmap3.GetPixel(m, l);
byte r = pixel2.R;
byte g = pixel2.G;
byte b = pixel2.B;
if ((r + g + b) / 3 < 128)
{
array[l * ((bitmap3.Width + 7) / 8) + m / 8] ^= (byte)(128 >> m % 8);
}
}
}
sendcommand(array);
sendcommand(CRLF_byte);
}
public void sendpicture(int xpoint, int ypoint, string filename)
{
Bitmap bitmap = new Bitmap(filename);
Bitmap bitmap2 = new Bitmap(bitmap.Width, bitmap.Height);
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
Color pixel = bitmap.GetPixel(i, j);
int num = (int)((double)(int)pixel.R * 0.3 + (double)(int)pixel.G * 0.59 + (double)(int)pixel.B * 0.11);
bitmap2.SetPixel(i, j, Color.FromArgb(pixel.A, num, num, num));
}
}
Bitmap bitmap3 = bitmap2;
new Bitmap(bitmap3.Width, bitmap3.Height);
sendcommandNOCRLF("BITMAP " + xpoint + "," + ypoint + "," + (bitmap3.Width + 7) / 8 + "," + bitmap3.Height + ", 0,");
byte[] array = new byte[(bitmap3.Width + 7) / 8 * bitmap3.Height];
for (int k = 0; k < array.Length; k++)
{
array[k] = byte.MaxValue;
}
for (int l = 0; l < bitmap3.Height; l++)
{
for (int m = 0; m < bitmap3.Width; m++)
{
Color pixel2 = bitmap3.GetPixel(m, l);
byte r = pixel2.R;
byte g = pixel2.G;
byte b = pixel2.B;
if ((r + g + b) / 3 < 128)
{
array[l * ((bitmap3.Width + 7) / 8) + m / 8] ^= (byte)(128 >> m % 8);
}
}
}
sendcommand(array);
sendcommand(CRLF_byte);
}
public void sendpicture(int xpoint, int ypoint, Bitmap original_picture)
{
Bitmap bitmap = new Bitmap(original_picture.Width, original_picture.Height);
for (int i = 0; i < original_picture.Width; i++)
{
for (int j = 0; j < original_picture.Height; j++)
{
Color pixel = original_picture.GetPixel(i, j);
int num = (int)((double)(int)pixel.R * 0.3 + (double)(int)pixel.G * 0.59 + (double)(int)pixel.B * 0.11);
bitmap.SetPixel(i, j, Color.FromArgb(pixel.A, num, num, num));
}
}
Bitmap bitmap2 = bitmap;
new Bitmap(bitmap2.Width, bitmap2.Height);
sendcommandNOCRLF("BITMAP " + xpoint + "," + ypoint + "," + (bitmap2.Width + 7) / 8 + "," + bitmap2.Height + ", 0,");
byte[] array = new byte[(bitmap2.Width + 7) / 8 * bitmap2.Height];
for (int k = 0; k < array.Length; k++)
{
array[k] = byte.MaxValue;
}
for (int l = 0; l < bitmap2.Height; l++)
{
for (int m = 0; m < bitmap2.Width; m++)
{
Color pixel2 = bitmap2.GetPixel(m, l);
byte r = pixel2.R;
byte g = pixel2.G;
byte b = pixel2.B;
if ((r + g + b) / 3 < 128)
{
array[l * ((bitmap2.Width + 7) / 8) + m / 8] ^= (byte)(128 >> m % 8);
}
}
}
sendcommand(array);
sendcommand(CRLF_byte);
}
public int sendfile(string path)
{
try
{
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] array = new byte[fileStream.Length];
int num = (int)fileStream.Length;
int offset = 0;
while (num > 0 && fileStream.Read(array, offset, num) != 0)
{
}
sendcommand(array);
}
}
catch (FileNotFoundException)
{
return -1;
}
return 1;
}
public int sendfile_NOCRLF(string path)
{
try
{
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] array = new byte[fileStream.Length];
int num = (int)fileStream.Length;
int offset = 0;
while (num > 0 && fileStream.Read(array, offset, num) != 0)
{
}
sendcommandNOCRLF(array);
}
}
catch (FileNotFoundException)
{
return -1;
}
return 1;
}
public int search_folder_filename(string condition)
{
string[] files = Directory.GetFiles(Environment.CurrentDirectory, condition);
foreach (string text in files)
{
if (text == "" || text == null)
{
return -1;
}
sendfile(text);
}
return 1;
}
public void send_bitmap(int x_axis, int y_axis, Bitmap bitmap_file)
{
sendpicture(x_axis, y_axis, bitmap_file);
}
private byte[] bit_array2byte_array(byte[] data)
{
int num = (data.Length + 7) / 8;
byte[] array = new byte[num];
for (int i = 0; i < num; i++)
{
array[i] = 0;
}
for (int j = 0; j <= data.Length - 1; j++)
{
if (data[j] == 1)
{
array[j / 8] ^= (byte)(128 >> j % 8);
}
}
return array;
}
}
}
\ No newline at end of file \ No newline at end of file
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
this.button4 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button5 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button();
this.btn_tsc = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
...@@ -113,11 +114,22 @@ ...@@ -113,11 +114,22 @@
this.button5.UseVisualStyleBackColor = true; this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click); this.button5.Click += new System.EventHandler(this.button5_Click);
// //
// btn_tsc
//
this.btn_tsc.Location = new System.Drawing.Point(154, 322);
this.btn_tsc.Name = "btn_tsc";
this.btn_tsc.Size = new System.Drawing.Size(123, 42);
this.btn_tsc.TabIndex = 8;
this.btn_tsc.Text = "TSC Print";
this.btn_tsc.UseVisualStyleBackColor = true;
this.btn_tsc.Click += new System.EventHandler(this.btn_tsc_Click);
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.btn_tsc);
this.Controls.Add(this.button5); this.Controls.Add(this.button5);
this.Controls.Add(this.pictureBox1); this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button4); this.Controls.Add(this.button4);
...@@ -146,6 +158,7 @@ ...@@ -146,6 +158,7 @@
private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button4;
private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button5; private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button btn_tsc;
} }
} }
using System; using Asa;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Net.NetworkInformation;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
...@@ -63,5 +65,19 @@ namespace PrintLabel_Test ...@@ -63,5 +65,19 @@ namespace PrintLabel_Test
text.Add("pn", "yumtr3123123j"); text.Add("pn", "yumtr3123123j");
pictureBox1.Image = print.PrintPreview(text); pictureBox1.Image = print.PrintPreview(text);
} }
private void btn_tsc_Click(object sender, EventArgs e)
{
Dictionary<string, string> text = new Dictionary<string, string>();
text.Add("ri", "123123123123123");
text.Add("pn", "yumtr3123123j");
print.SetResolution(300);
print.GetLabelName();
print.LoadLabel(textBox1.Text);
print.PrintToTsc1(text,out TscStauts tscStauts);
return;
}
} }
} }
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!