如何将 EPSON ESC/P 字节流转换为位图?

发布于 2024-12-01 03:57:30 字数 313 浏览 0 评论 0原文

我通过串行 COM 端口连接设备,从中获取 Epson ESC/P 格式的字符串。

是否可以使用此流提供某些 C# 类,然后将结果保存为图像 或者直接打印结果?

喜欢 System.Drawing.Printing.PrintDocument thePrintDocument = new System.Drawing.Printing.PrintDocument();

thePrintDocument.FeedWithESCP(myString); thePrintDocument.SaveThisAsPng(myFile);

问候斯特凡

I am interfacing a device via a serial COM port from which I get a string of characters in the Epson ESC/P format.

Is it possible to feed some C# class with this stream and then save the result as an image
or print the result directly ?

Like
System.Drawing.Printing.PrintDocument thePrintDocument = new System.Drawing.Printing.PrintDocument();

thePrintDocument.FeedWithESCP(myString);
thePrintDocument.SaveThisAsPng(myFile);

Regards Stefan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖果控 2024-12-08 03:57:30

这是一个简单的解决方案,它获取从 epson 打印机接收的字节。
这不是 Epson ESC/P 协议的完整实现,但对我来说已经足够了

byte[] myData 是从打印机接收的流

    public static Bitmap CreateImage(byte[] myData)
    {
        int i = 0;
        int ss = 0;
        float xpos = 0;
        float ESCKxpos = 0;
        float yposStep = 8;
        float ypos = 0;
        int p1 = 0;
        int p2 = 0;

        Font fn = null;
        Font fnw = null;
        Font fdw = null;
        Bitmap pg = new Bitmap(600, 600);
        Graphics gr = Graphics.FromImage(pg);
        SolidBrush sb = new SolidBrush(Color.Black);

        gr.FillRectangle(new SolidBrush(Color.White), 0, 0, pg.Width, pg.Height);

        xpos = 0;
        ypos = 0;

        try
        {
            i = 8;
            fnw = new Font("Courier New", i--, FontStyle.Regular);
            while (fnw.GetHeight() > 8)
                fnw = new Font("Courier New", i--, FontStyle.Regular);

            i = 16;
            fdw = new Font("Courier New", i--, FontStyle.Regular);
            while (fdw.GetHeight() > 16)
                fdw = new Font("Courier New", i--, FontStyle.Regular);


            fn = fnw;
            //
            // Read the stream of bytes, use a state machine 
            // to interpretate the various codes
            //
            i = 0;
            while (i < myData.Length)
            {
                switch (ss)
                {
                    case 0:
                        switch (myData[i])
                        {
                            case 10: // Line feed
                                ypos += yposStep;
                                break;

                            case 13: // Carrige return
                                xpos = 0;
                                break;

                            case 14: // SO
                                break;

                            case 27: // ESC sequence
                                ss = 100;
                                break;

                            default:
                                p1 = 0;
                                break;
                        }
                        break;

                    case 100:
                        //
                        // Escape sequence
                        //
                        switch (myData[i])
                        {
                            case (byte)'A': // ESC A, select line spacing
                                ss = 110;
                                break;

                            case (byte)'K': // ESC K, bitmap image
                                ss = 120;
                                break;

                            case (byte)'W': // ESC W, select font width
                                ss = 130;
                                break;

                            default:
                                p1 = 0;
                                break;
                        }
                        break;

                    case 110: // ESC A n, n/60-inch line spacing
                        // Ignore this
                        ss = 0;
                        break;

                    case 120: // ESC K, bitmap image
                        p1 = myData[i];
                        ss = 121;
                        break;

                    case 121: // ESC K, bitmap image
                        p2 = myData[i];
                        p2 *= 256;
                        p2 += p1;
                        if (p2 < 1)
                        {
                            ss = 0;
                        }
                        else
                        {
                            ESCKxpos = xpos;
                            ss = 122;
                        }
                        break;

                    case 122: // ESC K, bitmap image
                        {
                            int z;

                            for (int y = 0; y < 8; y++)
                            {
                                z = 1 << y;
                                if ((myData[i] & z) != 0)
                                {
                                    gr.FillRectangle(sb, xpos, ypos+(7 - y), 1, 1);
                                }
                            }
                            xpos++;
                            p2--;

                            if (p2 < 1)
                            {
                                ss = 0;
                                xpos = ESCKxpos;
                            }
                        }
                        break;


                    case 130:// ESC W n, Set the printer font to normal or double width
                        if ((myData[i] == 1) || (myData[i] == 49))
                        {
                            fn = fdw;
                            yposStep = 16;
                        }
                        else
                        {
                            fn = fnw;
                            yposStep = 8;
                        }
                        ss = 0;
                        break;

                }
                i++;
            }
        }
        catch { }



        return pg;

    } // public static Bitmap CreateImage(byte[] myData)

Here is a simple solution that takes the bytes received from the epson printer.
This is not a complete implementation of the Epson ESC/P protocol but it is enough for me

byte[] myData is the stream received from the printer

    public static Bitmap CreateImage(byte[] myData)
    {
        int i = 0;
        int ss = 0;
        float xpos = 0;
        float ESCKxpos = 0;
        float yposStep = 8;
        float ypos = 0;
        int p1 = 0;
        int p2 = 0;

        Font fn = null;
        Font fnw = null;
        Font fdw = null;
        Bitmap pg = new Bitmap(600, 600);
        Graphics gr = Graphics.FromImage(pg);
        SolidBrush sb = new SolidBrush(Color.Black);

        gr.FillRectangle(new SolidBrush(Color.White), 0, 0, pg.Width, pg.Height);

        xpos = 0;
        ypos = 0;

        try
        {
            i = 8;
            fnw = new Font("Courier New", i--, FontStyle.Regular);
            while (fnw.GetHeight() > 8)
                fnw = new Font("Courier New", i--, FontStyle.Regular);

            i = 16;
            fdw = new Font("Courier New", i--, FontStyle.Regular);
            while (fdw.GetHeight() > 16)
                fdw = new Font("Courier New", i--, FontStyle.Regular);


            fn = fnw;
            //
            // Read the stream of bytes, use a state machine 
            // to interpretate the various codes
            //
            i = 0;
            while (i < myData.Length)
            {
                switch (ss)
                {
                    case 0:
                        switch (myData[i])
                        {
                            case 10: // Line feed
                                ypos += yposStep;
                                break;

                            case 13: // Carrige return
                                xpos = 0;
                                break;

                            case 14: // SO
                                break;

                            case 27: // ESC sequence
                                ss = 100;
                                break;

                            default:
                                p1 = 0;
                                break;
                        }
                        break;

                    case 100:
                        //
                        // Escape sequence
                        //
                        switch (myData[i])
                        {
                            case (byte)'A': // ESC A, select line spacing
                                ss = 110;
                                break;

                            case (byte)'K': // ESC K, bitmap image
                                ss = 120;
                                break;

                            case (byte)'W': // ESC W, select font width
                                ss = 130;
                                break;

                            default:
                                p1 = 0;
                                break;
                        }
                        break;

                    case 110: // ESC A n, n/60-inch line spacing
                        // Ignore this
                        ss = 0;
                        break;

                    case 120: // ESC K, bitmap image
                        p1 = myData[i];
                        ss = 121;
                        break;

                    case 121: // ESC K, bitmap image
                        p2 = myData[i];
                        p2 *= 256;
                        p2 += p1;
                        if (p2 < 1)
                        {
                            ss = 0;
                        }
                        else
                        {
                            ESCKxpos = xpos;
                            ss = 122;
                        }
                        break;

                    case 122: // ESC K, bitmap image
                        {
                            int z;

                            for (int y = 0; y < 8; y++)
                            {
                                z = 1 << y;
                                if ((myData[i] & z) != 0)
                                {
                                    gr.FillRectangle(sb, xpos, ypos+(7 - y), 1, 1);
                                }
                            }
                            xpos++;
                            p2--;

                            if (p2 < 1)
                            {
                                ss = 0;
                                xpos = ESCKxpos;
                            }
                        }
                        break;


                    case 130:// ESC W n, Set the printer font to normal or double width
                        if ((myData[i] == 1) || (myData[i] == 49))
                        {
                            fn = fdw;
                            yposStep = 16;
                        }
                        else
                        {
                            fn = fnw;
                            yposStep = 8;
                        }
                        ss = 0;
                        break;

                }
                i++;
            }
        }
        catch { }



        return pg;

    } // public static Bitmap CreateImage(byte[] myData)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文