VB.NET 2008/VB - 인쇄

바코드 인쇄 - 바코드를 인쇄하는 소스 코드(Code 39)

본클라쓰 2012. 4. 5. 17:36

'# 바코드 39(1:2)를 생성하는 라이브리러

Public Class code39

    Private code39(90) As String
    Private WidthPerChar As Integer = 20
    Private barWidth As Integer = 2
    Private f As New Font("돋음", 13)

 

    Public Sub New()
        ' 이 호출은 디자이너에 필요합니다.
        InitializeComponent()
        ' InitializeComponent() 호출 뒤에 초기화 코드를 추가하십시오.
        Me.AllocateCode()
    End Sub

 

    ' 코드를 할당한다.
    Private Sub AllocateCode()
        ' 숫자
        code39(48) = "bbwBBb"   '0
        code39(49) = "BbwbbB"   '1
        code39(50) = "bBwbbB"   '2
        code39(51) = "BBwbbb"   '3
        code39(52) = "bbwBbB"   '4
        code39(53) = "BbwBbb"   '5
        code39(54) = "bBwBbb"   '6
        code39(55) = "bbwbBB"   '7
        code39(56) = "BbwbBb"   '8
        code39(57) = "bBwbBb"   '9

        ' 영문자
        code39(65) = "BbbwbB"      'A
        code39(66) = "bBbwbB"      'B
        code39(67) = "BBbwbb"      'C
        code39(68) = "bbBwbB"      'D
        code39(69) = "BbBwbb"      'E
        code39(70) = "bBBwbb"      'F
        code39(71) = "bbbwBB"      'G
        code39(72) = "BbbwBb"      'H
        code39(73) = "bBbwBb"      'I
        code39(74) = "bbBwBb"      'J
        code39(75) = "BbbbwB"      'K
        code39(76) = "bBbbwB"      'L
        code39(77) = "BBbbwb"      'M
        code39(78) = "bbBbwB"      'N
        code39(79) = "BbBbwb"      'O
        code39(80) = "bBBbwb"      'P
        code39(81) = "bbbBwB"      'Q
        code39(82) = "BbbBwb"      'R
        code39(83) = "bBbBwb"      'S
        code39(84) = "bbBBwb"      'T
        code39(85) = "BwbbbB"      'U
        code39(86) = "bwBbbB"      'V
        code39(87) = "BwBbbb"      'W
        code39(88) = "bwbBbB"      'X
        code39(89) = "BwbBbb"      'Y
        code39(90) = "bwBBbb"      'Z

        ' 특수문자
        code39(33) = "bwBbBb"    '!
        code39(35) = "bwbBBb"    '#
        code39(36) = "bwbwbwbb"    '$
        code39(37) = "bbwbwbwb"    '%
        code39(42) = "bwbBBb"     '*
        code39(43) = "bwbbwbwb"    '+
        code39(45) = "bwbbBB"    '-
        code39(46) = "BwbbBb"    '.
        code39(47) = "bwbwbbwb"    '/
    End Sub

 

    ' 바코드 이미지를 가져온다.
    Public Sub GetBarcode(ByVal g As Graphics, ByVal r As Rectangle, ByVal code As String)
        ' 입력받은 코드를 검증한다.
        Me.ValidateCode(code)

        ' 시작점의 x,y 포인트
        Dim startPoint As Point = New Point(r.X, r.Y)
        ' 그릴수 있는 공간의 너비
        Dim width As Integer = r.Width
        ' 그릴 수 있는 공간의 높이
        Dim height As Integer = r.Height
        Dim barLength As Integer = code.Length * WidthPerChar + (2 * WidthPerChar)

 

        If (width / barLength < 1) Then
            Throw New NotEnoughWidthException()
        End If

 

        ' 폰트의 사이즈를 구한다.
        Dim fontSize As Size = TextRenderer.MeasureText(code, Me.f)
        ' 바코드 바의 높이를 구한다.
        Dim barHeight As Integer = height - (fontSize.Height + 3)
        ' 바코드의 시작점을 구한다.
        startPoint.X = (width - barLength) / 2
        Me.DrawBarcode(g, code, startPoint, barHeight)

        ' 텍스트의 시작 포인트를 구한다.
        Dim fPoint As New Point((width - fontSize.Width) / 2, height - fontSize.Height)
        Me.DrawText(g, code, fPoint)

    End Sub

 

    ' 코드를 검증한다.
    Private Sub ValidateCode(ByVal code As String)
        Dim reg As New System.Text.Regularexpressions.Regex("^([\*\/\#\$]|[+-]|\w)*$")
        Dim match As System.Text.Regularexpressions.Match = reg.Match(code)
        If (Not match.Success) Then
            Throw New NotBarcodeFormat()
        End If
    End Sub

 

    ' 폰트를 그린다.
    Private Sub DrawText(ByVal g As Graphics, ByVal code As String, ByVal fPoint As Point)
        TextRenderer.DrawText(g, code, Me.f, fPoint, Color.Black)
        f.Dispose()
    End Sub

 

    ' 바코드 이미지를 그린다.
    Private Sub DrawBarcode(ByVal g As Graphics, ByVal code As String, ByVal startPoint As Point, ByVal barHeight As Integer)
        Dim dLineWidth As Integer = 1
        Dim x As Integer = startPoint.X
        Dim y As Integer = startPoint.Y

        ' 바코드 스캔이 가능하게 * 구분자를 넣는다.
        code = "*" & code & "*"

        ' 각 문자열을 모두 처리한다.
        For Each c As Char In code
            Dim val As Integer = AscW(c)
            Dim pattern As String = code39(val)

            ' 문자를 패턴에 따라 그린다.
            For Each i As Char In pattern
                Select Case i
                    Case "B"
                        g.FillRectangle(Brushes.Black, x, y, x + barWidth * 2, y + barHeight)
                        x = x + barWidth * 2
                    Case "b"
                        g.FillRectangle(Brushes.Black, x, y, x + barWidth, y + barHeight)
                        x = x + barWidth
                    Case "W"
                        g.FillRectangle(Brushes.White, x, y, x + barWidth * 2, y + barHeight)
                        x = x + barWidth * 2
                    Case "w"
                        g.FillRectangle(Brushes.White, x, y, x + barWidth, y + barHeight)
                        x = x + barWidth
                End Select
                ' 패턴을 그린 후 구분자를 그린다.
                g.FillRectangle(Brushes.White, x, y, x + dLineWidth, y + barHeight)
                x = x + dLineWidth
            Next
        Next

    End Sub

End Class

 

이 소스 코드가 컴파일된 dll 파일 (.NET 4.0 버전):

barcode.dll

 

 

사용방법은

 

Dim r As New Rectangle(0, 0, Me.Panel1.Size.Width, Me.Panel1.Size.Height)
Dim code39 As New barcode.code39
code39.GetBarcode(Me.Panel1.CreateGraphics, r, UCase(Me.TextBox1.Text))

 

barcode.dll
0.02MB