Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 772233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:46:37+00:00 2026-05-14T18:46:37+00:00

I am using the below mentioned library to create a barcode which is storing

  • 0

I am using the below mentioned library to create a barcode which is storing in a specified location as shown below:
My question is, is there a way instead of saving it to a png file I get byte data?

thanks

Code39 code = new Code39("10090");
        code.Paint().Save("c:/NewBARCODE.png", ImageFormat.Png);


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;

namespace BarCode39
{
    public class Code39Settings
    {
        private int height = 60;

        public int BarCodeHeight
        {
            get { return height; }
            set { height = value; }
        }

        private bool drawText = true;

        public bool DrawText
        {
            get { return drawText; }
            set { drawText = value; }
        }

        private int leftMargin = 10;

        public int LeftMargin
        {
            get { return leftMargin; }
            set { leftMargin = value; }
        }

        private int rightMargin = 10;

        public int RightMargin
        {
            get { return rightMargin; }
            set { rightMargin = value; }
        }

        private int topMargin = 10;

        public int TopMargin
        {
            get { return topMargin; }
            set { topMargin = value; }
        }

        private int bottomMargin = 10;

        public int BottomMargin
        {
            get { return bottomMargin; }
            set { bottomMargin = value; }
        }

        private int interCharacterGap = 2;

        public int InterCharacterGap
        {
            get { return interCharacterGap; }
            set { interCharacterGap = value; }
        }

        private int wideWidth = 2;

        public int WideWidth
        {
            get { return wideWidth; }
            set { wideWidth = value; }
        }

        private int narrowWidth = 1;

        public int NarrowWidth
        {
            get { return narrowWidth; }
            set { narrowWidth = value; }
        }

        private Font font = new Font(FontFamily.GenericSansSerif, 12);

        public Font Font
        {
            get { return font; }
            set { font = value; }
        }

        private int codeToTextGapHeight = 10;

        public int BarCodeToTextGapHeight
        {
            get { return codeToTextGapHeight; }
            set { codeToTextGapHeight = value; }
        }
    }
    public class Code39
    {
        #region Static initialization

        static Dictionary<char, Pattern> codes;

        static Code39()
        {
            object[][] chars = new object[][] 
            {
                new object[] {'0', "n n n w w n w n n"},
                new object[] {'1', "w n n w n n n n w"},
                new object[] {'2', "n n w w n n n n w"},
                new object[] {'3', "w n w w n n n n n"},
                new object[] {'4', "n n n w w n n n w"},
                new object[] {'5', "w n n w w n n n n"},
                new object[] {'6', "n n w w w n n n n"},
                new object[] {'7', "n n n w n n w n w"},
                new object[] {'8', "w n n w n n w n n"},
                new object[] {'9', "n n w w n n w n n"},
                new object[] {'A', "w n n n n w n n w"},
                new object[] {'B', "n n w n n w n n w"},
                new object[] {'C', "w n w n n w n n n"},
                new object[] {'D', "n n n n w w n n w"},
                new object[] {'E', "w n n n w w n n n"},
                new object[] {'F', "n n w n w w n n n"},
                new object[] {'G', "n n n n n w w n w"},
                new object[] {'H', "w n n n n w w n n"},
                new object[] {'I', "n n w n n w w n n"},
                new object[] {'J', "n n n n w w w n n"},
                new object[] {'K', "w n n n n n n w w"},
                new object[] {'L', "n n w n n n n w w"},
                new object[] {'M', "w n w n n n n w n"},
                new object[] {'N', "n n n n w n n w w"},
                new object[] {'O', "w n n n w n n w n"},
                new object[] {'P', "n n w n w n n w n"},
                new object[] {'Q', "n n n n n n w w w"},
                new object[] {'R', "w n n n n n w w n"},
                new object[] {'S', "n n w n n n w w n"},
                new object[] {'T', "n n n n w n w w n"},
                new object[] {'U', "w w n n n n n n w"},
                new object[] {'V', "n w w n n n n n w"},
                new object[] {'W', "w w w n n n n n n"},
                new object[] {'X', "n w n n w n n n w"},
                new object[] {'Y', "w w n n w n n n n"},
                new object[] {'Z', "n w w n w n n n n"},
                new object[] {'-', "n w n n n n w n w"},
                new object[] {'.', "w w n n n n w n n"},
                new object[] {' ', "n w w n n n w n n"},
                new object[] {'*', "n w n n w n w n n"},
                new object[] {'$', "n w n w n w n n n"},
                new object[] {'/', "n w n w n n n w n"},
                new object[] {'+', "n w n n n w n w n"},
                new object[] {'%', "n n n w n w n w n"}
            };

            codes = new Dictionary<char, Pattern>();
            foreach (object[] c in chars)
                codes.Add((char)c[0], Pattern.Parse((string)c[1]));
        }

        #endregion

        private static Pen pen = new Pen(Color.Black);
        private static Brush brush = Brushes.Black;

        private string code;
        private Code39Settings settings;

        public Code39(string code)
            : this(code, new Code39Settings())
        {
        }

        public Code39(string code, Code39Settings settings)
        {
            foreach (char c in code)
                if (!codes.ContainsKey(c))
                    throw new ArgumentException("Invalid character encountered in specified code.");

            if (!code.StartsWith("*"))
                code = "*" + code;
            if (!code.EndsWith("*"))
                code = code + "*";

            this.code = code;
            this.settings = settings;
        }

        public Bitmap Paint()
        {
            string code = this.code.Trim('*');

            SizeF sizeCodeText = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(code, settings.Font);

            int w = settings.LeftMargin + settings.RightMargin;
            foreach (char c in this.code)
                w += codes[c].GetWidth(settings) + settings.InterCharacterGap;
            w -= settings.InterCharacterGap;

            int h = settings.TopMargin + settings.BottomMargin + settings.BarCodeHeight;

            if (settings.DrawText)
                h += settings.BarCodeToTextGapHeight + (int)sizeCodeText.Height;

            Bitmap bmp = new Bitmap(w, h, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(bmp);

            int left = settings.LeftMargin;

            foreach (char c in this.code)
                left += codes[c].Paint(settings, g, left) + settings.InterCharacterGap;

            if (settings.DrawText)
            {
                int tX = settings.LeftMargin + (w - settings.LeftMargin - settings.RightMargin - (int)sizeCodeText.Width) / 2;
                if (tX < 0)
                    tX = 0;
                int tY = settings.TopMargin + settings.BarCodeHeight + settings.BarCodeToTextGapHeight;
                g.DrawString(code, settings.Font, brush, tX, tY);
            }

            return bmp;
        }

        private class Pattern
        {
            private bool[] nw = new bool[9];

            public static Pattern Parse(string s)
            {
                Debug.Assert(s != null);

                s = s.Replace(" ", "").ToLower();

                Debug.Assert(s.Length == 9);
                Debug.Assert(s.Replace("n", "").Replace("w", "").Length == 0);

                Pattern p = new Pattern();

                int i = 0;
                foreach (char c in s)
                    p.nw[i++] = c == 'w';

                return p;
            }

            public int GetWidth(Code39Settings settings)
            {
                int width = 0;

                for (int i = 0; i < 9; i++)
                    width += (nw[i] ? settings.WideWidth : settings.NarrowWidth);

                return width;
            }

            public int Paint(Code39Settings settings, Graphics g, int left)
            {
#if DEBUG
                Rectangle gray = new Rectangle(left, 0, GetWidth(settings), settings.BarCodeHeight + settings.TopMargin + settings.BottomMargin);
                g.FillRectangle(Brushes.Gray, gray);
#endif
                int x = left;

                int w = 0;
                for (int i = 0; i < 9; i++)
                {
                    int width = (nw[i] ? settings.WideWidth : settings.NarrowWidth);

                    if (i % 2 == 0)
                    {
                        Rectangle r = new Rectangle(x, settings.TopMargin, width, settings.BarCodeHeight);
                        g.FillRectangle(brush, r);
                    }

                    x += width;
                    w += width;
                }

                return w;
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:46:37+00:00Added an answer on May 14, 2026 at 6:46 pm

    Create a MemoryStream and save it to that instead. Then you can use ToArray() on the stream to get the data.

    byte[] bytes;
    using (var stream = new MemoryStream())
    {
        Code39 code = new Code39("10090");
        code.Paint().Save(stream, ImageFormat.Png);
        bytes = stream.ToArray();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a thumbnail loader in my project the one mentioned below. The problem
Currently i'm using below code which works well. $(#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel).hide(); any optimised code?
I am using below code to create a reminder in Google calendar (using Google
I am using below mentioned script to get href of new opened window, but
I am using the below mentioned code in my VB.net application to print two
I have a string as mentioned below: $ts = 3/11/09 11:18:59 AM; which I
When I run the below mentioned code using NetBeans, the allocated heap size graph
I was reading an article to disable validators in Javascript using below mentioned reference
i am new to this FCKeditor,i am using below mentioned code , i am
I am trying to register below mentioned javascript for using with GridView to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.