By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,662 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

Replacing a Filestream with a Memorystream

P: n/a
Hi all !

This message has been posted on .net.general, but I reposts it here due to lack of replies.

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 SendFileToPrinter 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 SendDocToPrinter procedure, but I don't know what. Any help are greatly
appreciated.

TIA
Best wishes
Kai Bohli
Norway

<My call to the SendDocToPrinter procedure>
private void btnPrint2_Click(object sender, System.EventArgs e)
{
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
MemoryStream memStrm = new MemoryStream();
StreamWriter sw = new StreamWriter(memStrm);
sw.WriteLine("\x02L");
sw.WriteLine("H07");
sw.WriteLine("D11");
sw.WriteLine("19110080100002510K OHM 1/4 WATT");
sw.WriteLine("1a6210000000050590PCS");
sw.WriteLine("E");
sw.WriteLine("");

LabelPrint.SendDocToPrinter(pd.PrinterSettings.Pri nterName,memStrm);
sw.Close();
}

</My call to the SendDocToPrinter procedure>

<My converted version>
public static bool SendDocToPrinter( 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 SendFileToPrinter( string szPrinterName, string szFileName )
{
// Open the file.
FileStream fs = new FileStream(szFileName, 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.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes);
return bSuccess;
}
</Orginal kb codep>

Best wishes
Kai Bohli
ka***********@online.no
Norway
Best wishes
Kai Bohli
ka***********@online.no
Norway
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Kai,

Did you set memorystream's Position to 0 before processing it?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Kai Bohli" <ka****@online.nospam> wrote in message
news:kn********************************@4ax.com...
Hi all !

This message has been posted on .net.general, but I reposts it here due to lack of replies.
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 SendFileToPrinter 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 SendDocToPrinter procedure, but I don't know what. Any help are greatly appreciated.

TIA
Best wishes
Kai Bohli
Norway

<My call to the SendDocToPrinter procedure>
private void btnPrint2_Click(object sender, System.EventArgs e)
{
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
MemoryStream memStrm = new MemoryStream();
StreamWriter sw = new StreamWriter(memStrm);
sw.WriteLine("\x02L");
sw.WriteLine("H07");
sw.WriteLine("D11");
sw.WriteLine("19110080100002510K OHM 1/4 WATT");
sw.WriteLine("1a6210000000050590PCS");
sw.WriteLine("E");
sw.WriteLine("");

LabelPrint.SendDocToPrinter(pd.PrinterSettings.Pri nterName,memStrm);
sw.Close();
}

</My call to the SendDocToPrinter procedure>

<My converted version>
public static bool SendDocToPrinter( 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 SendFileToPrinter( string szPrinterName, string szFileName ) {
// Open the file.
FileStream fs = new FileStream(szFileName, 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.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes);
return bSuccess;
}
</Orginal kb codep>

Best wishes
Kai Bohli
ka***********@online.no
Norway
Best wishes
Kai Bohli
ka***********@online.no
Norway

Nov 15 '05 #2

P: n/a
Thanks for your reply Miha,it's really appreciated.

I didn't do that. I've tried it now like this:
memStrm.Position = 0;
LabelPrint.SendDocToPrinter(pd.PrinterSettings.Pri nterName,memStrm);

or should I set the position to 0 before I start writing with the StreamWriter ?

I couldn't get it to work. The thing here is that I'm writing to a labelprinter and that thing is
touchy about getting linefeeds after each command etc. The memorystream must do something that the
filestream dosen't.
On Sun, 8 Feb 2004 09:59:57 +0100, "Miha Markic [MVP C#]" <miha at rthand com> wrote:
Hi Kai,
Did you set memorystream's Position to 0 before processing it?

Best wishes
Kai Bohli
ka***********@online.no
Norway
Nov 15 '05 #3

P: n/a
Hi Kai,

After you've finished writting to StreamWriter make sure you invoke its
Flush method.
sw.WriteLine...
sw.Flush();
ms.Position = 0;
--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Kai Bohli" <ka****@online.nospam> wrote in message
news:8n********************************@4ax.com...
Thanks for your reply Miha,it's really appreciated.

I didn't do that. I've tried it now like this:
memStrm.Position = 0;
LabelPrint.SendDocToPrinter(pd.PrinterSettings.Pri nterName,memStrm);

or should I set the position to 0 before I start writing with the StreamWriter ?
I couldn't get it to work. The thing here is that I'm writing to a labelprinter and that thing is touchy about getting linefeeds after each command etc. The memorystream must do something that the filestream dosen't.
On Sun, 8 Feb 2004 09:59:57 +0100, "Miha Markic [MVP C#]" <miha at rthand

com> wrote:
Hi Kai,
Did you set memorystream's Position to 0 before processing it?

Best wishes
Kai Bohli
ka***********@online.no
Norway

Nov 15 '05 #4

P: n/a
Hi Miha !

That did it ! It seem that I have to send sw.Flush after each command though. You're my hero !
Thank you, thank you, thank you ! :)

On Sun, 8 Feb 2004 12:24:56 +0100, "Miha Markic [MVP C#]" <miha at rthand com> wrote:
Hi Kai,

After you've finished writting to StreamWriter make sure you invoke its
Flush method.
sw.WriteLine...
sw.Flush();
ms.Position = 0;


Best wishes
Kai Bohli
ka***********@online.no
Norway
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.