barnhill / barcodelib

C# Barcode Image Generation Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Escape sequences

ArpadHUTI opened this issue · comments

Dear Brad,

I'm a newbie in GitHUb, so sorry if I cause inconvenience to you, but I don't know how can I ask you at this site....

So I would like to use this library, but I would like to know how to handle escape sequences, just like (\t) and (\n).
Can you help me in this case?

Thank you so much! Have a nice day!
Kind Regrads,
Árpi

Hello,

What would you like to do with escape sequences?

First, it is important to understand why programming languages use escape sequences in the first place. In C#, which imitates C, a string literal is written as a double quote (") followed by a series of characters and escape sequences and then another double quote ("). What happens if you want to have a string container a double quote? Simply writing a double quote in the middle of the string will indicate to the compiler that the string ends where that double quote is. Thus, there is an escape sequence \" which is used to indicate that the string should contain a double quote. But what if you wanted to write a string which had a backslash (\) followed by a double quote (")? Because the backslash has special behavior now, another escape sequence, \\, was added which is used to write a backslash. Now you can write a string literal which has a backslash followed by a double quote: "\\\"".

Within many barcode encodings, including Code 128 (the linked Wikipedia article is a useful resource), backslash can be represented as a character as well as a double quote. So you can have a barcode like this:

backslash-doublequote

If you were writing a C# program to create this barcode, you could read from a file which has a backslash followed by a double quote in it:

backslash-doublequote.txt:

\"

Program.cs:

using BarcodeLib;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace BarcodeLibNetFxPlay
{
    class Program
    {
        static void Main(string[] args)
        {
            var backslashDoublequote = File.ReadAllText("backslash-doublequote.txt");
            var barcode = new Barcode
            {
                BarWidth = 4,
            };
            using (var barcodeImage = barcode.Encode(TYPE.CODE128, backslashDoublequote))
            {
                SaveWithWhiteBorder(barcodeImage, "backslash-doublequote.png");
            }
        }

        static void SaveWithWhiteBorder(Image image, string path)
        {
            // Add white sides to make scannable even when viewing against a dark background.
            using (var boxedImage = new Bitmap(image.Width + 80, image.Height))
            {
                using (var borderedImageGraphics = Graphics.FromImage(boxedImage))
                {
                    borderedImageGraphics.Clear(Color.White);
                    borderedImageGraphics.DrawImageUnscaled(image, 40, 0);
                }
                boxedImage.Save(path, ImageFormat.Png);
            }
        }
    }
}

Or, if you wanted to have the backslash followed by a double quote written directly in your program, you can use two escape sequences: \\ for the backslash and \" for the double quote. See below:

Program.cs:

using BarcodeLib;
using System.Drawing;
using System.Drawing.Imaging;

namespace BarcodeLibNetFxPlay
{
    class Program
    {
        static void Main(string[] args)
        {
            var backslashDoublequote = "\\\"";
            var barcode = new Barcode
            {
                BarWidth = 4,
            };
            using (var barcodeImage = barcode.Encode(TYPE.CODE128, backslashDoublequote))
            {
                SaveWithWhiteBorder(barcodeImage, "backslash-doublequote.png");
            }
        }

        static void SaveWithWhiteBorder(Image image, string path)
        {
            // Add white sides to make scannable even when viewing against a dark background.
            using (var boxedImage = new Bitmap(image.Width + 80, image.Height))
            {
                using (var borderedImageGraphics = Graphics.FromImage(boxedImage))
                {
                    borderedImageGraphics.Clear(Color.White);
                    borderedImageGraphics.DrawImageUnscaled(image, 40, 0);
                }
                boxedImage.Save(path, ImageFormat.Png);
            }
        }
    }
}

Now, if you are really asking about encoding newlines and line feeds and characters other than double quote which are often represented in C# using escape sequences, those are supported by various barcode types too. Again, Code 128 documents support for all of ASCII including NUL, LF (line feed, represented in C# as '\n'), and CR (carriage return, represented in C# as '\r').

If you need any further help, please clarify your question or ask.

Additionally, this sort of question might be better suited to be asked on Stack Overflow. If you have a question specifically about how to use BarcodeLib, then here is probably good too.

Thanks!