CPCL命令打印24位bmp

本文提供了一种读取24位BMP图像文件的方法,并通过C#代码实现将图像转换为十六进制字符串,便于进一步处理或传输。该方法适用于需要将图像数据转化为文本形式的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看到很多朋友跟我遇到相似的问题,我把我当初的解决办法贴出来吧。下面的代码是读取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;




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值