CPCL命令打印24位bmp

来源:互联网 发布:js md5算法 编辑:程序博客网 时间:2024/06/12 00:18

看到很多朋友跟我遇到相似的问题,我把我当初的解决办法贴出来吧。下面的代码是读取24位bmp文件的方法(距离写这个代码有一段时间了,有些注释掉的代码已经忘了干嘛的了)

private static string get24BitBmpData(string filePath){    Bitmap bmp = new Bitmap(filePath);    byte[] bitArray = { 128, 64, 32, 16, 8, 4, 2, 1 };    string imgTxt = "";    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);    BitmapData data = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);    IntPtr firstPix = data.Scan0;    int rowByteCount = bmp.Width * 3;    int filledCount = data.Stride - rowByteCount;    int bytes = data.Stride * data.Height;//Math.Ceiling((double)bmp.Width / 8)    byte[] rgbValues = new byte[bytes];    System.Runtime.InteropServices.Marshal.Copy(firstPix, rgbValues, 0, bytes);    int printRowByteCount = Convert.ToInt32(Math.Ceiling((double)(bmp.Width) / 8));    int printRowByteFillCount = 4 - (printRowByteCount % 4);    //int bitFillCount = 8 - (bmp.Width % 8);    byte[] printData = new byte[(printRowByteCount + printRowByteFillCount) * bmp.Height];    int byteCount = 0;    int bitCount = 0;    int rowPoint = 0;    for (int i = 0; i < rgbValues.Length; i += 3)    {        int rgbValue = rgbValues[i] + rgbValues[i + 1] + rgbValues[i + 2];        if (rgbValue != (255 * 3))        {            printData[byteCount] = Convert.ToByte(printData[byteCount] | bitArray[bitCount]);        }        if (bitCount == 7)        {            bitCount = 0;            byteCount++;        }        else        {            bitCount++;        }        if ((rowPoint + 3) == rowByteCount)        {            rowPoint = 0;            if (bitCount > 0)            {                byteCount++;            }            bitCount = 0;            byteCount += printRowByteFillCount;            //if (bitCount + filledCount <= 7)            //{            //    bitCount += filledCount;            //}            //else            //{            //    bitCount = filledCount - 8 + bitCount;            //    byteCount++;            //}            i = i + filledCount;        }        else        {            rowPoint += 3;        }    }    foreach (byte byteData in printData)    {        string hexStr = Convert.ToString(byteData, 16);        if (hexStr.Length == 1)        {            hexStr = '0' + hexStr;        }        imgTxt += hexStr;    }    bmp.UnlockBits(data);    return imgTxt.ToUpper();}


调用这个方法,连接打印机传输命令就行了。

string CRNL = "\r\n";
string imgTxt = get24BitBmpData("xxxx.bmp");
string cmddata = "! 0 200 200 300 1" + CRNL +
                "EG " + 24 + " " + 50 + " 10 10 " + imgTxt + CRNL + 
                "FORM" + CRNL +
                "PRINT" + CRNL;




原创粉丝点击