Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Printing Barcode number in c sharp through ZDesigner S4M-203dpi ZPL

runtime printing

  • Please log in to reply
4 replies to this topic

#1 Sreekar

Sreekar

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 05 July 2010 - 10:29 PM

hi,
In button click of one button i have wrote c sharp code to print barcode number. My Barcode printer is connected through USB cable to my system. When i connected it through LPT1 cable it is getting printing. But after changing from LPT1 cable to USB cable my c sharp code is getting the following error:
Invalid handle.
Parameter name: handle
Can u suggest me how to solve this problem.
Below is the code which i wrote in C sharp.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.IO;

using System.IO.Ports;
using Microsoft.Win32.SafeHandles;
using System.Web;
using System.Runtime.InteropServices;


namespace TTDGODOWN
{

	
	public partial class BarcodeNo : Form

	{
	  
		Boolean dup = false;

		int Lbl_cnt = 1;
		[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
	static extern SafeFileHandle CreateFile(
		string fileName,
		[MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
		[MarshalAs(UnmanagedType.U4)] FileShare fileShare,
		IntPtr securityAttributes,
		[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
		[MarshalAs(UnmanagedType.U4)] FileAttributes flags,
		IntPtr template);
		
		public byte[] ConvertStringToByteArray(string stringToConvert)
		{
			return (new UnicodeEncoding()).GetBytes(stringToConvert);
		}
		
		SqlConnection ObjSqlConnection;
		SqlCommand ObjSqlCommand;
		private string connectionstring = @"Initial Catalog=TTDPRESS;" +
		@"Data Source=10.1.3.21;" +
		@"User ID=sa;" +
		@"Password=tirumala";
		public BarcodeNo()
		{

			InitializeComponent();
		}


  private void button2_Click(object sender, EventArgs e)
		{						  
			DateTime Dt = DateTime.Today;
				String BarcodeID;
				BarcodeID = listBox1.SelectedItem.ToString();
				SafeFileHandle printer = CreateFile("USB001", FileAccess.ReadWrite, 0, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
				FileStream FS = new FileStream(printer, FileAccess.ReadWrite);													  
				String HumanReadable;
				HumanReadable = listBox1.SelectedItem.ToString();
				BinaryReader Br = new BinaryReader(FS);		  
				Byte[] Buff = new Byte[1024];
				Buff = System.Text.Encoding.Unicode.GetBytes("^XA");
				FS.Write(Buff, 0, Buff.Length);
				Buff = System.Text.Encoding.Unicode.GetBytes("^PRC");
				FS.Write(Buff, 0, Buff.Length);
				Buff = System.Text.Encoding.Unicode.GetBytes("^LL240");
				FS.Write(Buff, 0, Buff.Length);
				Buff = System.Text.Encoding.Unicode.GetBytes("^MD6");
				FS.Write(Buff, 0, Buff.Length);
				Buff = System.Text.Encoding.Unicode.GetBytes("^MMT");
				FS.Write(Buff, 0, Buff.Length);
				Buff = System.Text.Encoding.Unicode.GetBytes("^MNY");
				FS.Write(Buff, 0, Buff.Length);
				//Buff = System.Text.Encoding.Unicode.GetBytes("^XA");
				//FS.Write(Buff, 0, Buff.Length);
				String ItemName;
				ItemName = listBox2.Text.ToString();
				//''''''''''''''''''''''''''''''''''''''''''''''''''LEFT,TOP,FONTHEIGHT,FONTSIZE,
				Buff = System.Text.Encoding.Unicode.GetBytes("^FO10,07^A0N,18,33^CI13^FR^FD" + "T.T.Devasthanams" + "^FS");
				FS.Write(Buff, 0, Buff.Length);
				//Buff = System.Text.Encoding.Unicode.GetBytes("^FO10,70^A0N,22,33^CI13^FR^FD"+ItemName+"^FS");
				//FS.Write(ConvertStringToByteArray("^FO10,70^A0N,22,33^CI13^FR^FD" + ItemName + "^FS"), 0, Buff.Length);
				//Buff = System.Text.Encoding.Unicode.GetBytes("^FO10,90^A0N,22,23^CI13^FR^FD" + Dt + "^FS");
				//FS.Write(Buff, 0, Buff.Length);
				if (dup == true)
				{
					Buff = System.Text.Encoding.Unicode.GetBytes("^FO10,110^A0N,27,23^CI13^FR^FD" + "DUPLICATE-TICKET" + "^FS");
					FS.Write(Buff, 0, Buff.Length);
				}
				else
				{
					Buff = System.Text.Encoding.Unicode.GetBytes("^FO10,110^A0N,27,23^CI13^FR^FD" + "BarcodeNo." + HumanReadable + "^FS");
					FS.Write(Buff, 0, Buff.Length);
				}
				Buff = System.Text.Encoding.Unicode.GetBytes("^BY2,3.0^FO55,134^BCN,80,N,Y,N^FR^FD>;" + BarcodeID + "^FS");
				FS.Write(Buff, 0, Buff.Length);  
				Buff = System.Text.Encoding.Unicode.GetBytes("^PQ1,0,0,N");
				FS.Write(Buff, 0, Buff.Length);
				Buff = System.Text.Encoding.Unicode.GetBytes("^XZ");
				FS.Write(Buff, 0, Buff.Length);
				Buff = System.Text.Encoding.Unicode.GetBytes("^FX End of job");
				FS.Write(Buff, 0, Buff.Length);
				Buff = System.Text.Encoding.Unicode.GetBytes("^XA");
				FS.Write(Buff, 0, Buff.Length);
				Buff = System.Text.Encoding.Unicode.GetBytes("^IDR:ID*.*");
				FS.Write(Buff, 0, Buff.Length);
				Buff = System.Text.Encoding.Unicode.GetBytes("^XZ");
				FS.Write(Buff, 0, Buff.Length);
				
				//bool bSuccess = false;
				//IntPtr pUnmanagedBytes = new IntPtr(0);
				//int nLength;
				//nLength = Convert.ToInt32(1024);
				//Buff = Br.ReadBytes(nLength);
				//pUnmanagedBytes =
				//Marshal.Copy(Buff, 0, pUnmanagedBytes, nLength);				
				Br.Close();
				FS.Close();
				Lbl_cnt = Lbl_cnt + 1;
				
			}
		}

	  
		
		}

  • 0

#2 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 06 July 2010 - 12:02 PM

Please use code tags like this: [code.] put your code here [/code.] but don't add the periods, that will make it much easier for people to read your code, and much more likely to give an answer. ;)
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#3 AspperWhite

AspperWhite

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Programming Language:C++
  • Learning:C++, C#, Visual Basic .NET

Posted 28 November 2012 - 07:16 PM

I will show some codes in C# I used and hope it helps. This is the one I encode Code 39.

 

BarCode code39 = new BarCode();
code39.Symbology = Symbology.Code39;
code39.Data = "CODE39";
code39.BarcodeImageWidth = 175;
code39.BarcodeImageHeight = 50;
code39.N = 2;
code39.I = 5;
code39.AutoResize = true;
code39.draw("...\code39_csharp.gif");

My barcodes created can be scanned correctly.


Edited by Roger, 15 October 2013 - 10:02 AM.
added codetags

  • 0

#4 petersae

petersae

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 09 May 2013 - 01:56 AM

Here share the sample Code for creating Code 39 in C# that I have used:

using System;
using System.Collections.Generic;
using System.Text;
using OnBarcode.Barcode;
using System.Drawing.Imaging;
using System.Drawing;


Linear code39 = new Linear();

// Barcode data to encode
code39.Data = "ONBARCODE";
// Barcode symbology type
code39.Type = BarcodeType.CODE39;
// Apply checksum digit for Code-39
code39.AddCheckSum = true;

// The space between 2 characters in code 39; This a multiple of X; The default is 1.
code39.I = 1.0f;
// Wide/narrow ratio, 2.0 - 3.0 inclusive, default is 2.
code39.N = 2.0f;
// If true, display a * in the beginning and end of barcode text
code39.ShowStartStopInText = false;

/*
* Barcode Image Related Settings
*/
// Unit of meature for all size related setting in the library. 
code39.UOM = UnitOfMeasure.PIXEL;
// Bar module width (X), default is 1 pixel;
code39.X = 1;
// Bar module height (Y), default is 60 pixel;
code39.Y = 60;
// Barcode image left, right, top, bottom margins. Defaults are 0.
code39.LeftMargin = 0;
code39.RightMargin = 0;
code39.TopMargin = 0;
code39.BottomMargin = 0;
// Image resolution in dpi, default is 72 dpi.
code39.Resolution = 72;
// Created barcode orientation.
//4 options are: facing left, facing right, facing bottom, and facing top
code39.Rotate = Rotate.Rotate0;

/*
* Linear barcodes human readable text styles
*/
// Display human readable text under the barcode
code39.ShowText = true;
// Display checksum digit at the end of barcode data.
code39.ShowCheckSumChar = true;
// Human readable text font size, font family and style
code39.TextFont = new Font("Arial", 9f, FontStyle.Regular);
// Space between barcode and text. Default is 6 pixel.
code39.TextMargin = 6;

// Generate Code-39 and encode barcode to gif format
code39.Format = System.Drawing.Imaging.ImageFormat.Gif;
code39.drawBarcode("C:\\code39.gif");

/*
You can also call other drawing methods to generate barcodes

public void drawBarcode(Graphics graphics);

public void drawBarcode(string filename);

public Bitmap drawBarcode();

public void drawBarcode(Stream stream);

*/

For more detailed information, please refer to this passage called guide of creating barcode in C#.


Edited by Roger, 15 October 2013 - 10:02 AM.
added codetags

  • 0

#5 sarahwhite0

sarahwhite0

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 26 May 2014 - 06:17 PM

For more detailed information, please refer to this passage called guide of creating barcode in C#.

Hi, i read the c# guide you provided. And it is great. But i want to know whether the barcode software can be used free for education orgnization.


  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download