ConvertBarcode.cs 5.1 KB
using System;
using System.Collections.Generic;
using System.Drawing;
using Model;

namespace BLL
{
    public static class ConvertBarcode
    {
        private static int width;
        private static int height;
        private static string text;
        private static readonly ZXing.BarcodeWriter writer = new();
        private static readonly Dictionary<PrintLabelFieldType, Func<Bitmap>> code = new()
        {
            { PrintLabelFieldType.Text, Text },
            { PrintLabelFieldType.Code39, Code39 },
            { PrintLabelFieldType.Code93, Code93 },
            { PrintLabelFieldType.Code128, Code128 },
            { PrintLabelFieldType.DataMatrix, DATA_MATRIX },
            { PrintLabelFieldType.QRCode, QR_CODE },
            { PrintLabelFieldType.PDF417, PDF_417 }
        };


        public static Bitmap StrToCode(PrintLabelFieldType type, string s, SizeF size)
        {
            text = s;
            width = Convert.ToInt32(size.Width);
            height = Convert.ToInt32(size.Height);
            if (string.IsNullOrEmpty(text))
                return null;
            return code[type].Invoke();
        }



        private static Bitmap Text()
        {
            return null;
        }

        private static Bitmap Code39()
        {
            try
            {
                ZXing.Common.EncodingOptions opt = new()
                {
                    Width = width,
                    Height = height,
                    Margin = 2,
                    PureBarcode = true
                };
                writer.Options = opt;
                writer.Format = ZXing.BarcodeFormat.CODE_39;
                return writer.Write(text);
            }
            catch (Exception ex)
            {
                LogNet.log.Error("Code39", ex);
                return null;
            }
        }

        private static Bitmap Code93()
        {
            try
            {
                ZXing.Common.EncodingOptions opt = new()
                {
                    Width = width,
                    Height = height,
                    Margin = 2,
                    PureBarcode = true
                };
                writer.Options = opt;
                writer.Format = ZXing.BarcodeFormat.CODE_93;
                return writer.Write(text);
            }
            catch (Exception ex)
            {
                LogNet.log.Error("Code93", ex);
                return null;
            }
        }

        private static Bitmap Code128()
        {
            try
            {
                ZXing.Common.EncodingOptions opt = new()
                {
                    Width = width,
                    Height = height,
                    Margin = 2,
                    PureBarcode = true
                };
                writer.Options = opt;
                writer.Format = ZXing.BarcodeFormat.CODE_128;
                return writer.Write(text);
            }
            catch (Exception ex)
            {
                LogNet.log.Error("Code128", ex);
                return null;
            }
        }

        private static Bitmap DATA_MATRIX()
        {
            try
            {
                ZXing.Datamatrix.DatamatrixEncodingOptions opt = new()
                {
                    Width = width,
                    Height = height,
                    Margin = 2,
                    PureBarcode = true
                };
                writer.Options = opt;
                writer.Format = ZXing.BarcodeFormat.DATA_MATRIX;
                return writer.Write(text);
            }
            catch (Exception ex)
            {
                LogNet.log.Error("DATA_MATRIX", ex);
                return null;
            }
        }

        private static Bitmap QR_CODE()
        {
            try
            {
                ZXing.QrCode.QrCodeEncodingOptions opt = new()
                {
                    DisableECI = true,
                    CharacterSet = "UTF-8",
                    //Width = width,
                    //Height = height,
                    Width = 300,
                    Height = 300,
                    Margin = 1,
                    PureBarcode = true
                };
                writer.Options = opt;
                writer.Format = ZXing.BarcodeFormat.QR_CODE;
                return writer.Write(text);
            }
            catch (Exception ex)
            {
                LogNet.log.Error("QR_CODE", ex);
                return null;
            }
        }

        private static Bitmap PDF_417()
        {
            try
            {
                ZXing.PDF417.PDF417EncodingOptions opt = new()
                {
                    DisableECI = true,
                    CharacterSet = "UTF-8",
                    Width = 100,
                    //Height = height,
                    Margin = 2,
                    PureBarcode = true
                };
                writer.Options = opt;
                writer.Format = ZXing.BarcodeFormat.PDF_417;
                return writer.Write(text);
            }
            catch (Exception ex)
            {
                LogNet.log.Error("PDF_417", ex);
                return null;
            }
        }


    }
}