473,326 Members | 2,010 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,326 software developers and data experts.

PCL printing over sockets

I want to print a bitmap directly to a network printer using a socket.
To do this I use PCL codes to set the printer in the right mode and
print the image.
Note that this code is used on the Compact Framework, so there is no
PrintDocument or even a driver I can use.
try

{

// Connect to printer.

s = new Socket(AddressFamily.InterNetwork,

SocketType.Stream, ProtocolType.IP);

IPAddress addr = IPAddress.Parse("10.0.0.18");

IPEndPoint ipep = new IPEndPoint(addr, 9100);

s.Connect(ipep);

Bitmap bmp = new Bitmap(appPath + "images\\Chart_PHT.gif");

System.IO.MemoryStream msBitmap = new System.IO.MemoryStream();

bmp.Save(msBitmap, System.Drawing.Imaging.ImageFormat.Gif);

System.Drawing.Imaging.BitmapData bmpd = bmp.LockBits(new Rectangle(0,
0, bmp.Width, bmp.Height),

System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format24bppRgb) ;
IntPtr ptr = bmpd.Scan0;

// Declare an array to hold the bytes of the bitmap.

// This code is specific to a bitmap with 24 bits per pixels.

int bytes = bmp.Width * bmp.Height * 3;

byte[] rgbValues = new byte[bytes];

// Copy the RGB values into the array.

System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

// Unlock the bits.

bmp.UnlockBits(bmpd);

// reset printer

byte[] byteReset = new byte[] { 027, 069 };

s.Send(byteReset, 0, 2, SocketFlags.None);

// set resolution

byte[] byteRes = new byte[] { 027, 042, 116, 051, 048, 048, 082 }; //
300 DPI

s.Send(byteRes, 0, 7, SocketFlags.None);

// start raster graphics

byte[] byteStart = new byte[] { 027, 042, 114, 049, 065 };

s.Send(byteStart, 0, 5, SocketFlags.None);

// set compression

byte[] byteComp = new byte[] { 027, 042, 098, 048, 077 }; //
uncompressed

s.Send(byteComp, 0, 5, SocketFlags.None);

// send graphics

for (int i = 0; i < (rgbValues.Length); i += (bmp.Width * 3))

{

byte[] byteSend1 = new byte[] { 027, 042, 098 };

byte[] byteSend2 = BitConverter.GetBytes(bmp.Width * 3);

byte[] byteSend3 = null;

if ((i + (bmp.Width * 3)) < rgbValues.Length)

byteSend3 = new byte[] { 086 };

else

byteSend3 = new byte[] { 087 }; // for last row

byte[] byteSendAll = new byte[byteSend1.Length + byteSend2.Length +
byteSend3.Length + (bmp.Width * 3)];

Buffer.BlockCopy(byteSend1, 0, byteSendAll, 0, byteSend1.Length);

Buffer.BlockCopy(byteSend2, 0, byteSendAll, byteSend1.Length,
byteSend2.Length);

Buffer.BlockCopy(byteSend3, 0, byteSendAll, byteSend1.Length +
byteSend2.Length, byteSend3.Length);

Buffer.BlockCopy(rgbValues, i, byteSendAll, byteSend1.Length +
byteSend2.Length + byteSend3.Length, bmp.Width * 3);

s.Send(byteSendAll, SocketFlags.None);

}

// end raster graphics

byte[] byteEnd = new byte[] { 027, 042, 114, 098, 067 };

s.Send(byteEnd, 0, 5, SocketFlags.None);

}

finally

{

s.Close();

}

Sending out the PCL codes (arrays starting with 027) works excellent.
But the printing of the actual data does not work yet.

The rgbValues array now has the RGB values of all pixels (so three
bytes used for each pixel). But when I send that straight after the
start printing command it does not print anything on the paper.
I do send it to the open socket line by line as suggested by the
documentation I found, but that didn't help either.

Does anyone have a clue about what format to use to send the bitmap
data to the printer?

Mar 8 '06 #1
0 1377

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

Similar topics

2
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
1
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
0
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
4
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
3
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly...
0
by: notregister | last post by:
Ji, i tried to use tcpclient to send a series of PCL command to a printer, but after going thru this, it print out a series of junk...the same series work well with another code i written for...
3
by: J C | last post by:
Hi, I'm using UDPClient to make a simple DNS server. I notice that intermittently and unpredictibly I get: Unhandled Exception: System.Net.Sockets.SocketException: An existing connection...
7
by: Adam01 | last post by:
Im using cygwin to test the code of a server I am writing. I've included sys/types.h, sys/socket.h, netdb.h, and arpa/inet.h. And this is the output.. ../../../sockets.cpp: In constructor...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.