473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Replacing a Filestream with a Memorystream

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 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
Nov 22 '05 #1
2 6040
Cor
Hi Kai,

did you ask this question also in the newsgroup

microsoft.public.dotnet.languages.csharp

I think that in that newsgroup you have a better change to get your anser

Cor
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

Nov 22 '05 #2
Hi Cor.

Thanks, I'll try that.

Hi Kai,
did you ask this question also in the newsgroup
microsoft.public.dotnet.languages.csharp
I think that in that newsgroup you have a better change to get your anser
Cor


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Toby Mathews | last post by:
Hi, In an ASP.Net application I want to convert open create a FileStream object from a System.Drawing.Image - is this possible? I create an instance of an Image object using the FromFile method,...
2
by: Michel Racicot | last post by:
Hi there, I need to know what is the fastest way to copy a 9mb FileStream into a MemoryStream? Must I read a big buffer then writting it in my MemoryStream or is it a better way? Thank you
4
by: Kai Bohli | last post by:
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...
5
by: Chris Fink | last post by:
How do I load a string into a FileStream without going to disk? For example, string abc = "This is a string"; How do I load abc into a FileStream? FileStream input = new FileStream(.....);
9
by: Tim | last post by:
Hi, I have a list of products in with images stored in a SQL Server 2000 DB. I need to be able to retrieve the images when a product is chose. I use the code below to get the image from the DB...
3
by: sbparsons | last post by:
I have a file opened as a FileStream. I have a StreamReader and StreamWriter object opened, referencing the FileStream object. My aim is to read lines from the file until I find the line where...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
1
by: pgmfan | last post by:
What is the difference among filestream, memoryStream and byte stream, how do i use each properly. i confused by them appreciate for any advice ,please help
1
by: ad | last post by:
I do know how to convert a FileStream to MemoryStream. Could somebody help me?
3
by: paradigmshift | last post by:
I am looking at how to move data from a file to a memory stream. This data will be needed many times over and over so i don't want to have to rely on the system to keep the information cashed for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.