Bitmap printing through a NetworkStream

  • Thread starter Thread starter mottebelke
  • Start date Start date
M

mottebelke

I have a bitmap I want to print on a network printer using a network
stream.
This is the code I'm currently using:

chartBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

byte[] printChart = ms.GetBuffer();

TcpClient client = new TcpClient("10.0.0.18", 9100);

NetworkStream printStream = client.GetStream();

printStream.Write(printChart, 0, printChart.Length);

printStream.Flush();

printStream.Close();


This method is working fine when I use text, but printing a bitmap will
give me one line with all kinds of characters.
I read about having to use special characters to let the printer know
it has to print a bitmap. Is that something I have to set in here, or
am I missing something else?
 
I have a bitmap I want to print on a network printer using a network
stream.
This is the code I'm currently using:

chartBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
byte[] printChart = ms.GetBuffer();
TcpClient client = new TcpClient("10.0.0.18", 9100);
NetworkStream printStream = client.GetStream();
printStream.Write(printChart, 0, printChart.Length);
printStream.Flush();
printStream.Close();

This method is working fine when I use text, but printing a bitmap will
give me one line with all kinds of characters.
I read about having to use special characters to let the printer know
it has to print a bitmap. Is that something I have to set in here, or
am I missing something else?

That will depend on the printer, unfortunately. You should probably use
the normal .NET printing services.

Note, however, that your use of MemoryStream is incorrect. Calling
GetBuffer() will give you "spare" bytes at the end. You should either
use ToArray(), or use GetBuffer() in conjunction with the Length
property to work out how much to actually use.

Jon
 
|I have a bitmap I want to print on a network printer using a network
| stream.
| This is the code I'm currently using:
|
| chartBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
|
| byte[] printChart = ms.GetBuffer();
|
| TcpClient client = new TcpClient("10.0.0.18", 9100);
|
| NetworkStream printStream = client.GetStream();
|
| printStream.Write(printChart, 0, printChart.Length);
|
| printStream.Flush();
|
| printStream.Close();
|
|
| This method is working fine when I use text, but printing a bitmap will
| give me one line with all kinds of characters.
| I read about having to use special characters to let the printer know
| it has to print a bitmap. Is that something I have to set in here, or
| am I missing something else?
|

That's the task of the printer driver for that kind of printer. Why are you
passing data directly to a printer server without using the drive anyway.
Maybe that it works for simple ASCII streams but anything else will fail to
print.

Willy.
 
Back
Top