C# 熱敏印表機 Socket 網路連結 列印 圖片 (一)

來源:互聯網
上載者:User

標籤:data   timeout   pre   prot   ++   摘要   box   socket   ipaddr   

using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;namespace POSPrinter{    ///   <summary>     ///   POSPrinter的摘要說明。    ///   此類處理網路列印,使用了IP連接埠.    ///   </summary>     public class NetPOSPrinter     {        string ipPort = "127.0.0.1";        public NetPOSPrinter()        {        }        public NetPOSPrinter(string IpPort)        {            this.ipPort = IpPort;//印表機連接埠         }        ///   <summary>         ///   輸出文字到印表機         ///   </summary>         ///   <param   name= "str "> 要列印的內容 </param>         public void PrintLine(string str)        {            //建立串連            IPAddress ipa = IPAddress.Parse(ipPort);            IPEndPoint ipe = new IPEndPoint(ipa, 9100);//9100為小票印表機指定連接埠            Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            soc.Connect(ipe);            //string str= "hello,123456789,大家好! ";            byte[] b = System.Text.Encoding.GetEncoding("GB2312").GetBytes(str);            soc.Send(b);            soc.Close();        }        public void PrintPic(Bitmap bmp)        {            //把ip和連接埠轉化為IPEndPoint執行個體            IPEndPoint ip_endpoint = new IPEndPoint(IPAddress.Parse(ipPort), 9100);            //建立一個Socket            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            //串連到伺服器            socket.Connect(ip_endpoint);            //應對同步Connect逾時過長的辦法,猜測應該是先用非同步方式建立以個串連然後,            //確認串連是否可用,然後報錯或者關閉後,重建立立一個同步串連                                //socket.SendTimeout = 1000;            //初始化印表機,並列印            Byte[] byte_send = Encoding.GetEncoding("gb18030").GetBytes("\x1b\x40");            //發送測試資訊            socket.Send(byte_send, byte_send.Length, 0);            byte[] data = new byte[] { 0x1B, 0x33, 0x00 };            socket.Send(data, data.Length, 0);            data[0] = (byte)‘\x00‘;            data[1] = (byte)‘\x00‘;            data[2] = (byte)‘\x00‘;    // Clear to Zero.            Color pixelColor;            // ESC * m nL nH 點陣圖            byte[] escBmp = new byte[] { 0x1B, 0x2A, 0x00, 0x00, 0x00 };            escBmp[2] = (byte)‘\x21‘;            //nL, nH            escBmp[3] = (byte)(bmp.Width % 256);            escBmp[4] = (byte)(bmp.Width / 256);            // data            for (int i = 0; i < (bmp.Height / 24) + 1; i++)            {                socket.Send(escBmp, escBmp.Length, 0);                for (int j = 0; j < bmp.Width; j++)                {                    for (int k = 0; k < 24; k++)                    {                        if (((i * 24) + k) < bmp.Height)   // if within the BMP size                        {                            pixelColor = bmp.GetPixel(j, (i * 24) + k);                            if (pixelColor.R == 0)                            {                                data[k / 8] += (byte)(128 >> (k % 8));                            }                        }                    }                    socket.Send(data, 3, 0);                    data[0] = (byte)‘\x00‘;                    data[1] = (byte)‘\x00‘;                    data[2] = (byte)‘\x00‘;    // Clear to Zero.                }                                byte_send = Encoding.GetEncoding("gb18030").GetBytes("\n");                //發送測試資訊                socket.Send(byte_send, byte_send.Length, 0);            } // data            byte_send = Encoding.GetEncoding("gb18030").GetBytes("\n");            //發送測試資訊            socket.Send(byte_send, byte_send.Length, 0);            socket.Close();        }        ///   <summary>         ///   開啟錢箱         ///   </summary>         public void OpenCashBox()        {            IPAddress ipa = IPAddress.Parse(ipPort);            IPEndPoint ipe = new IPEndPoint(ipa, 9100);//9100為小票印表機指定連接埠            Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            soc.Connect(ipe);            char[] c = { Convert.ToChar(27), ‘p‘, Convert.ToChar(0), Convert.ToChar(60), Convert.ToChar(255) };            byte[] b = System.Text.Encoding.GetEncoding("GB2312").GetBytes(c);            soc.Send(b);            soc.Close();        }    }}

  轉自:http://www.cnblogs.com/rinack/p/4838211.html

C# 熱敏印表機 Socket 網路連結 列印 圖片 (一)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.