본문 바로가기

Programing/.NET

[C#] BarcodeLib 바코드 생성

NuGet 패키지 관리자에서 BarCodeLib 참조 추가

https://github.com/barnhill/barcodelib

 

GitHub - barnhill/barcodelib: C# Barcode Image Generation Library

C# Barcode Image Generation Library. Contribute to barnhill/barcodelib development by creating an account on GitHub.

github.com

 

바코드 생성후 이미지 파일로 저장

public static void GetBarcode(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image, string fileSaveUrl)
        {
            try
            {
                image = null;

                BarcodeLib.Barcode b = new BarcodeLib.Barcode();
                b.BackColor = System.Drawing.Color.White;//이미지 배경 컬러
                b.ForeColor = System.Drawing.Color.Black;//바코드 색상
                b.IncludeLabel = true;
                b.Alignment = BarcodeLib.AlignmentPositions.LEFT;
                b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;//표시 위치
                b.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;//이미지 포맷
                System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);//글꼴 설정
                b.LabelFont = font;
                b.Height = height;//세로사이즈
                b.Width = width;//가로사이즈

                image = b.Encode(type, code);//생성
                image.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Png);

            }
            catch (Exception err)
            {
                err.ToString();
                image = null;
            }
        }

 

바코드 생성후 Stream 으로 변환하여 파일업로드

   public void GetBarCode(string code)
        {
            Barcode barcodeLib = new Barcode();

            // Define basic settings of the image
            int imageWidth = 290;
            int imageHeight = 120;
            Color foreColor = Color.Black;
            Color backColor = Color.White; 
            string data = code;

            // Generate the barcode with your settings
            Image barcodeImage = barcodeLib.Encode(TYPE.UPCA, data, foreColor, backColor, imageWidth, imageHeight);
            Byte[] imgByte = barcodeLib.Encoded_Image_Bytes;

            Stream stream = new MemoryStream(imgByte);
            //스트림으로 파일 전송
            blobStorageUpload(stream, "fileName");
        }

 

'Programing > .NET' 카테고리의 다른 글

[C#] Azure BlobStorage 파일 업로드  (0) 2021.11.11
[C#]SFTP 파일 업로드  (0) 2021.11.08
[C#] AES256(AES/CBC/PKCS5Padding) 암/복호화  (0) 2021.03.14
[C#] RestSharp 으로 HTTP/HTTPS 통신  (0) 2020.07.24
[C#] Log파일 생성  (0) 2019.07.30