C # the bar code is usually printed using commands or images. The format of the picture is fastreport, but vs is not found to call it. It is only successfully used in Delphi 7. In fact, the vast majority of bar code printer manufacturers have their own printing command language. With this language, they can directly print without a driver, and the operation is very simple, just send the command to the printer.
VS has a comport control, but there is no ready-made LPT port control. Compared with the comport, The LPT speed is faster. Therefore, when printing, the customer generally chooses the LPT communication mode, after some consultation on the Internet, the LPT port printing is finally realized, and the printer is Zebra, which is written to share with you.
C # example of bar code printing:
- UsingSystem;
- UsingSystem. Collections. Generic;
- UsingSystem. componentmodel;
- UsingSystem. Data;
- UsingSystem. drawing;
- UsingSystem. text;
- UsingSystem. Windows. forms;
- // C # instance for bar code printing
- NamespacePrintdemo
- {
- PublicPartialClassForm1: Form
- {
- PublicForm1 ()
- {
- Initializecomponent ();
- }
-
- Private VoidForm1_load (ObjectSender, eventargs E)
- {
- Tbbarcode. Focus ();
- }
- // C # instance for bar code printing
- Private VoidTbbarcode_keydown (ObjectSender,
-
- Keyeventargs E)
- {
- Switch(E. keycode)
- {
- CaseKeys. Enter:
- Printbarcode (tbbarcode. Text. Trim ());
- Tbbarcode. Text = "";
- Tbbarcode. Focus ();
- Break;
- Default:
- Break;
- }
- }
- Private VoidPrintbarcode (StringBarcode)
- {
- Barcode = "^ XA ^ fo48, 12 ^ by4 ^ BCN, 152, N, N ^ FD>;" +
- // C # instance for bar code printing
- Barcode. Trim () + "^ FS ^ ft62, 220 ^ ci0 ^ ABN, 44,28 ^ FD" +
-
- Barcode. Trim () + "^ FS ^ pq1, 0, 1, y ^ xz ";
- Printdemo. posprinter PRN =New
-
- Printdemo. posprinter ("LPT1 ");
- StringStrmsg = PRN. printline (barcode );
- If(Strmsg! = "")
- {
- MessageBox. Show (strmsg );
- }
- }
- }
- }
C # define posprinter for bar code printing as follows
- NamespacePrintdemo
- {
- ClassPosprinter
- {
- Const IntOpen_existing = 3;
- StringPrnport = "LPT1 ";
- [Dllimport ("kernel32.dll", charset = charset. Auto)]
- Private Static ExternIntptr createfile (String
-
- Lpfilename,
- IntDwdesiredaccess,
- IntDww.mode,
- IntLpsecurityattributes,
- IntDwcreationdisposition,
- IntDwflagsandattributes,
- IntHtemplatefile );
-
- PublicPosprinter ()
- {
- //
- // Todo: add the constructor logic here
- //
- }
-
- PublicPosprinter (StringPrnport)
- {
- This. Prnport = prnport; // printer port
- }
-
- Public StringPrintline (StringStr)
- {
-
- Intptr ihandle = createfile (prnport, 0x40000000,
-
- 0, 0, open_existing, 0, 0 );
- If(Ihandle. toint32 () =-1)
- {
- Return"LPT1 port open failed ";
- }
- Else
- {
-
- Filestream FS =NewFilestream (ihandle,
-
- Fileaccess. readwrite );
- Streamwriter Sw =NewStreamwriter (FS,
-
- System. Text. encoding. Default); // C # print the written data of the bar code operation
- Sw. writeline (STR );
- Sw. Close ();
- FS. Close ();
- Return"";
- }
- }
- }
- }
I will introduce you to the example of C # print bar code operation. I hope you can understand and learn about C # print bar code operation.