9 Replies - 177 Views - Last Post: 02 February 2012 - 04:34 PM Rate Topic: -----

Topic Sponsor:

#1 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 55
  • View blog
  • Posts: 250
  • Joined: 12-January 11

Generating Barcodes in .NET?

Posted 02 February 2012 - 02:15 PM

I have been looking around the web to find out if there is a way to generate barcodes (using a the code39 encoding), but have been unable to find anywhere that gives a sufficiently thorough example. Anyone have any suggestions?
Is This A Good Question/Topic? 0
  • +

Replies To: Generating Barcodes in .NET?

#2 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: Generating Barcodes in .NET?

Posted 02 February 2012 - 02:18 PM

You could download a free 3of9 font and use it to print your barcode.

I think you just have to use * as a prefix and suffix of the string you're barcoding for the start/stop character of the barcode.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suiter #2
  • member icon


Reputation: 3556
  • View blog
  • Posts: 14,985
  • Joined: 12-June 08

Re: Generating Barcodes in .NET?

Posted 02 February 2012 - 02:19 PM

Really? A few things popped up - one being a stackoverflow post from 2008 that had pretty much all the code dropped on there..

Have you thought about using just the barcode 39 font?
Was This Post Helpful? 0
  • +
  • -

#4 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 55
  • View blog
  • Posts: 250
  • Joined: 12-January 11

Re: Generating Barcodes in .NET?

Posted 02 February 2012 - 02:33 PM

I found that a few times, but I'm not sure if that will cause me any problems later on. The sites that mentioned using the font said you have to be careful when using the fonts because making them "too big" would result in a blurry, unreadable barcode. I don't know enough about them to really be able to make an educated decision.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suiter #2
  • member icon


Reputation: 3556
  • View blog
  • Posts: 14,985
  • Joined: 12-June 08

Re: Generating Barcodes in .NET?

Posted 02 February 2012 - 02:45 PM

Quote

I'm not sure if that will cause me any problems later on.

Huh?
Was This Post Helpful? 0
  • +
  • -

#6 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 55
  • View blog
  • Posts: 250
  • Joined: 12-January 11

Re: Generating Barcodes in .NET?

Posted 02 February 2012 - 03:01 PM

View Postmodi123_1, on 02 February 2012 - 02:45 PM, said:

Quote

I'm not sure if that will cause me any problems later on.

Huh?


Using the font instead of generating lines/boxes/whatever.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • Coder-ian
  • member icon

Reputation: 1401
  • View blog
  • Posts: 7,358
  • Joined: 29-May 08

Re: Generating Barcodes in .NET?

Posted 02 February 2012 - 03:26 PM

View PostPsyguy, on 02 February 2012 - 11:01 PM, said:

View Postmodi123_1, on 02 February 2012 - 02:45 PM, said:

Quote

I'm not sure if that will cause me any problems later on.

Huh?


Using the font instead of generating lines/boxes/whatever.

So what's a font if it not lines/boxes/whatever.
Was This Post Helpful? 0
  • +
  • -

#8 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 55
  • View blog
  • Posts: 250
  • Joined: 12-January 11

Re: Generating Barcodes in .NET?

Posted 02 February 2012 - 03:29 PM

Ya, I guess, but dont the edges of a premade font get distorted if you expand them to be viewed larger than they are intended? Again, I'm not the most knowledgeable when it comes to scalability of different graphical objects and the ins/outs of font use.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • Coder-ian
  • member icon

Reputation: 1401
  • View blog
  • Posts: 7,358
  • Joined: 29-May 08

Re: Generating Barcodes in .NET?

Posted 02 February 2012 - 03:53 PM

To me Bar-Codes are primarily an input (a way to refer to some data).
The bar-code inherently doesn't encode any data about what it refer to. (Eg the Product's Price)

Displaying barcode (to me) make little sense, unless you're printing it out
Was This Post Helpful? 0
  • +
  • -

#10 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 55
  • View blog
  • Posts: 250
  • Joined: 12-January 11

Re: Generating Barcodes in .NET?

Posted 02 February 2012 - 04:34 PM

I found this link to a C# control that will generate barcodes for you. I am currently trying to see if it will work for my purposes...I'll probably have to change it a little to accommodate more information. I pasted the VB.NET translation below, although there is some exception being thrown in the paint event, but having a try/catch with no action taken if an exception occurs seems to have stalled the problem:
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms

Public Class Barcode
    Inherits System.Windows.Forms.UserControl

    Public Enum AlignType
        Left
        Center
        Right
    End Enum

    Public Enum BarCodeWeight
        Small = 1
        Medium
        Large
    End Enum

    Private panel1 As System.Windows.Forms.Panel
    Private printDocument1 As System.Drawing.Printing.PrintDocument
    Private components As System.ComponentModel.Container = Nothing
    Private align As AlignType = AlignType.Center
    Private code As String = "1234567890"
    Private leftMargin As Integer = 10
    Private topMargin As Integer = 10
    Private cheight As Integer = 50
    Private showHeader As Boolean
    Private showFooter As Boolean
    Private headerText As String = "BarCode Demo"
    Private weight As BarCodeWeight = BarCodeWeight.Small
    Private headerFont As Font = New Font("Courier", 18)
    Private footerFont As Font = New Font("Courier", 8)

    Public Property _VertAlign As AlignType
        Get
            Return align
        End Get
        Set(value As AlignType)
            align = value
            panel1.Invalidate()
        End Set
    End Property
    Public Property BarCode As String
        Get
            Return code
        End Get
        Set(value As String)
            code = value.ToUpper
            panel1.Invalidate()
        End Set
    End Property
    Public Property BarCodeHeight As Integer
        Get
            Return cheight
        End Get
        Set(value As Integer)
            cheight = value
            panel1.Invalidate()
        End Set
    End Property
    Public Property _LeftMargin As Integer
        Get
            Return leftMargin
        End Get
        Set(value As Integer)
            leftMargin = value
            panel1.Invalidate()
        End Set
    End Property
    Public Property _TopMargin As Integer
        Get
            Return topMargin
        End Get
        Set(value As Integer)
            topMargin = value
            panel1.Invalidate()
        End Set
    End Property
    Public Property _ShowHeader As Boolean
        Get
            Return showHeader
        End Get
        Set(value As Boolean)
            showHeader = value
            panel1.Invalidate()
        End Set
    End Property
    Public Property _ShowFooter As Boolean
        Get
            Return showFooter
        End Get
        Set(value As Boolean)
            showFooter = value
            panel1.Invalidate()
        End Set
    End Property
    Public Property _HeaderText As String
        Get
            Return headerText
        End Get
        Set(value As String)
            headerText = value
            panel1.Invalidate()
        End Set
    End Property
    Public Property _Weight As BarCodeWeight
        Get
            Return weight
        End Get
        Set(value As BarCodeWeight)
            weight = value
            panel1.Invalidate()
        End Set
    End Property
    Public Property _HeaderFont As Font
        Get
            Return headerFont
        End Get
        Set(value As Font)
            headerFont = value
            panel1.Invalidate()
        End Set
    End Property
    Public Property _FooterFont As Font
        Get
            Return footerFont
        End Get
        Set(value As Font)
            footerFont = value
            panel1.Invalidate()
        End Set
    End Property

    Public Sub New()
        InitializeComponent()
    End Sub

    Public Sub Print()
        Dim pd As New PrintDialog
        pd.Document = printDocument1

        If pd.ShowDialog = DialogResult.OK Then
            pd.document.Print()
        End If
    End Sub

    '''<summary>
    '''Clean up any resources being used.
    '''</summary>
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)

        If disposing = True Then
            If Not components Is Nothing Then components.Dispose()
        End If

        MyBase.Dispose(disposing)

    End Sub

#Region "Component Designer generated code"
    ''' <summary>
    ''' Required method for Designer support - do not modify 
    ''' the contents of this method with the code editor.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InitializeComponent()
        Me.panel1 = New System.Windows.Forms.Panel()
        Me.printDocument1 = New System.Drawing.Printing.PrintDocument()
        Me.SuspendLayout()
        ' 
        'panel1
        ' 
        Me.panel1.BackColor = System.Drawing.SystemColors.Window
        Me.panel1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.panel1.Location = New System.Drawing.Point(0, 0)
        Me.panel1.Name = "panel1"
        Me.panel1.Size = New System.Drawing.Size(424, 240)
        Me.panel1.TabIndex = 0
        AddHandler Me.panel1.Paint, New System.Windows.Forms.PaintEventHandler(AddressOf panel1_Paint)
        ' 
        ' printDocument1
        '
        AddHandler Me.printDocument1.PrintPage, New System.Drawing.Printing.PrintPageEventHandler(AddressOf printDocument1_PrintPage)
        ' 
        ' BarCodeCtrl
        ' 
        Me.Controls.Add(Me.panel1)
        Me.Name = "BarCodeCtrl"
        Me.Size = New System.Drawing.Size(424, 240)
        AddHandler Me.Resize, New System.EventHandler(AddressOf BarCodeCtrl_Resize)
        Me.ResumeLayout(False)
    End Sub
#End Region

    Dim alphabet39 As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"
    Dim coded39Char As String() = {
   "000110100",
   "100100001",
   "001100001",
   "101100000",
   "000110001",
   "100110000",
   "001110000",
   "000100101",
   "100100100",
   "001100100",
   "100001001",
   "001001001",
   "101001000",
   "000011001",
   "100011000",
   "001011000",
   "000001101",
   "100001100",
   "001001100",
   "000011100",
   "100000011",
   "001000011",
   "101000010",
   "000010011",
   "100010010",
   "001010010",
   "000000111",
   "100000110",
   "001000110",
   "000010110",
   "110000001",
   "011000001",
   "111000000",
   "010010001",
   "110010000",
   "011010000",
   "010000101",
   "110000100",
   "011000100",
   "010101000",
   "010100010",
   "010001010",
   "000101010",
   "010010100"
  }

    Private Sub panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs)
        Try
            Dim intercharacterGap As String = "0"
            Dim str As String = "*" & code.ToUpper() & "*"
            Dim strLength As Integer = str.Length

            For i As Integer = 0 To code.Length - 1
                If (alphabet39.IndexOf(code(i)) = -1) OrElse (code(i) = "*") Then
                    e.Graphics.DrawString("INVALID BAR CODE TEXT", Font, Brushes.Red, 10, 10)
                    Return
                End If
            Next
            Dim encodedString = String.Empty

            For i As Integer = 0 To strLength - 1
                If i > 0 Then
                    encodedString += intercharacterGap
                    encodedString += coded39Char(alphabet39.IndexOf(str(i)))
                End If
            Next

            Dim encodedStringLength As Integer = encodedString.Length
            Dim widthOfBarCodeString As Integer = 0
            Dim wideToNarrowRatio As Double = 3

            If Not align = AlignType.Left Then
                For i As Integer = 0 To encodedStringLength - 1
                    If encodedString(i) = "1" Then
                        widthOfBarCodeString += CInt(wideToNarrowRatio * CInt(weight))
                    Else
                        widthOfBarCodeString += CInt(weight)
                    End If
                Next
            End If
            Dim x As Integer = 0
            Dim wid As Integer = 0
            Dim yTop As Integer = 0
            Dim hSize As SizeF = e.Graphics.MeasureString(headerText, headerFont)
            Dim fSize As SizeF = e.Graphics.MeasureString(code, footerFont)
            Dim headerX As Integer = 0
            Dim footerX As Integer = 0

            If align = AlignType.Left Then
                x = leftMargin
                headerX = leftMargin
                footerX = leftMargin
            ElseIf align = AlignType.Center Then
                x = (Width - widthOfBarCodeString) / 2
                headerX = (Width - CInt(hSize.Width)) / 2
                footerX = (Width - CInt(fSize.Width)) / 2
            Else
                x = Width - widthOfBarCodeString - leftMargin
                headerX = Width - CInt(hSize.Width) - leftMargin
                footerX = Width - CInt(fSize.Width) - leftMargin
            End If
            If showHeader = True Then
                yTop = CInt(hSize.Height) + topMargin
                e.Graphics.DrawString(headerText, headerFont, Brushes.Black, headerX, topMargin)
            Else
                yTop = topMargin
            End If
            For i As Integer = 0 To encodedStringLength - 1
                If encodedString(i) = "1" Then
                    wid = CInt(wideToNarrowRatio * CInt(weight))
                Else
                    wid = CInt(weight)
                    e.Graphics.FillRectangle(IIf(i Mod 2 = 0, Brushes.Black, Brushes.White), x, yTop, wid, cheight)
                    x += wid
                End If
            Next
            yTop += cheight

            If showFooter = True Then e.Graphics.DrawString(code, footerFont, Brushes.Black, footerX, yTop)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub printDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs)
        panel1_Paint(sender, New PaintEventArgs(e.Graphics, Me.ClientRectangle))
    End Sub

    Public Sub SaveImage(ByVal file As String)

        Dim bmp As New Bitmap(Width, cheight, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        Dim g As Graphics = Graphics.FromImage(bmp)

        g.FillRectangle(Brushes.White, 0, 0, Width, cheight)
        panel1_Paint(vbNull, New PaintEventArgs(g, Me.ClientRectangle))
        bmp.Save(file)

    End Sub

    Private Sub BarCodeCtrl_Resize(sender As Object, e As System.EventArgs)

        panel1.Invalidate()

    End Sub

End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1