473,698 Members | 2,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

did you ask this question also in the newsgroup

microsoft.publi c.dotnet.langua ges.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 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

Nov 22 '05 #2
Hi Cor.

Thanks, I'll try that.

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


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

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

Similar topics

7
14242
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, then use the GetThumbnailImage method to create a second Image. This second one is the one I want to get a FileStream from, so that I can then use its Handle to use Response.WriteFile to output the thumbnail on my ASP.Net page. Any help would...
2
5363
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
9223
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 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...
5
12736
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
1891
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 and assign it to the picture box. It works fine the first time but subsequent times it gives me an error saying that the file is in use by another process. What process? If I go to the folder and try to delete the file in windows I get the same...
3
8297
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 there is a unique comment as a place holder. I would then like to use the StreamWriter to insert lines after this comment and save the file. How do I instruct the StreamWriter to find the line position based on the
12
5886
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: <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish> etc. I want to remove the "g:\" from the file paths. I wrote a console app that successfully reads the file and writes a duplicate of it, but fails for some reason to do the "replacing" of the "g:\". The code...
1
6594
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
13778
by: ad | last post by:
I do know how to convert a FileStream to MemoryStream. Could somebody help me?
3
4655
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 access. Kind of an easy question for pros but thats why I figured I would ask here. Thanks,
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7729
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.