Hi all !
This is my first day with Visual Studio and C#, and I'm trying to send "raw data" to the printer
port. I found the SendFileToPrint er example below in a knowledge base at MS site
It works, but I need to replace the filestream with a memorystream cause there's no need to write
files and then load them again just to print.
So I rewrote the procedure and the call. The problem is of course that it dosen't work. If I replace
the MemoryStream (with FileStream) in btnPrint event, and save the result to a file it's ok. I can
copy the file directly to the port, and the printer prints ok. I know that I'm doing something wrong
in my version of the SendDocToPrinte r procedure, but I don't know what. Any help are greatly
appreciated.
TIA
Best wishes
Kai Bohli
Norway
<My call to the SendDocToPrinte r procedure>
private void btnPrint2_Click (object sender, System.EventArg s e)
{
PrintDialog pd = new PrintDialog();
pd.PrinterSetti ngs = new PrinterSettings ();
if (DialogResult.O K == pd.ShowDialog(t his))
{
MemoryStream memStrm = new MemoryStream();
StreamWriter sw = new StreamWriter(me mStrm);
sw.WriteLine("\ x02L");
sw.WriteLine("H 07");
sw.WriteLine("D 11");
sw.WriteLine("1 911008010000251 0K OHM 1/4 WATT");
sw.WriteLine("1 a62100000000505 90PCS");
sw.WriteLine("E ");
sw.WriteLine("" );
LabelPrint.Send DocToPrinter(pd .PrinterSetting s.PrinterName,m emStrm);
sw.Close();
}
</My call to the SendDocToPrinte r procedure>
<My converted version>
public static bool SendDocToPrinte r( string szPrinterName, MemoryStream ms)
{ // just the lines that I've changed are present here.
BinaryReader br = new BinaryReader(ms );
Byte []bytes = new Byte[ms.Length];
nLength = Convert.ToInt32 (ms.Length);
</My converted version>
<Orginal kb code>
public static bool SendFileToPrint er( string szPrinterName, string szFileName )
{
// Open the file.
FileStream fs = new FileStream(szFi leName, FileMode.Open);
// Create a BinaryReader on the file.
BinaryReader br = new BinaryReader(fs );
// Dim an array of bytes big enough to hold the file's contents.
Byte []bytes = new Byte[fs.Length];
bool bSuccess = false;
// Your unmanaged pointer.
IntPtr pUnmanagedBytes = new IntPtr(0);
int nLength;
nLength = Convert.ToInt32 (fs.Length);
// Read the contents of the file into the array.
bytes = br.ReadBytes( nLength );
// Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCo TaskMem(nLength );
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(by tes, 0, pUnmanagedBytes , nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrin ter(szPrinterNa me, pUnmanagedBytes , nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoT askMem(pUnmanag edBytes);
return bSuccess;
}
</Orginal kb codep>
Best wishes
Kai Bohli
ka***********@o nline.no
Norway