
Den folgenden Quelltext habe ich vor etwa 2 Jahren in seiner ursprünglichen Form in einem VBA-Forum gefunden und an VB.NET angepasst um in einer ASP.NET-Anwendung on-the-fly Barcodes auszugeben.
Vorteil von Code39 ist seine große Druck- und Lesetoleranz. Zwischenzeitlich hatte ich den Sourcecode auch einmal in C# umgeschrieben. Dieser ist allerdings bei einem Festplattendefekt verloren gegangen.
Private Function MD_Barcode39(ByVal Barcode As String, ByVal PaintObj As Object, _ ByVal mLeft As Single, ByVal mTop As Single, _ ByVal mWidth As Single, ByVal mHeight As Single) Dim Nbar As Single, Wbar As Single, Qbar As Single, NextBar As Single Dim CountX As Single, CountY As Single Dim Parts As Single, Pix As Single, BarCodePlus As String Dim Stripes As String, BarType As String Dim Mx As Single, my As Single, Sx As Single, Sy As Single Const Nratio = 20, Wratio = 55, Qratio = 35 Dim g As System.Drawing.Graphics Dim pB As New System.Drawing.SolidBrush(System.Drawing.Color.Black) Dim pW As New System.Drawing.SolidBrush(System.Drawing.Color.White) Dim color As System.Drawing.SolidBrush 'Get control size and location properties. Sx = mLeft Sy = mTop Mx = mWidth my = mHeight g = PaintObj 'Calculate actual and relative pixels values. Parts = (Barcode.Length + 2) * ((6 * Nratio) + (3 * Wratio) + (1 * Qratio)) Pix = (Mx / Parts) Nbar = (20 * Pix) Wbar = (55 * Pix) Qbar = (35 * Pix) 'Initialize bar index and color. NextBar = Sx color = pW 'Pad each end of string with start/stop characters. BarCodePlus = "*" & Barcode.ToUpper & "*" 'Walk through each character of the barcode contents. For CountX = 0 To BarCodePlus.Length - 1 'Get Barcode 1/0 string for indexed character. Stripes = MD_BC39(BarCodePlus.Substring(CountX, 1)) For CountY = 0 To 8 'For each 1/0, draw a wide/narrow bar. BarType = Stripes.Substring(CountY, 1) 'Toggle the color (black/white). If color Is pW Then color = pB Else color = pW End If Select Case BarType Case "1" 'Draw a wide bar. g.FillRectangle(color, NextBar, Sy, Wbar + NextBar, my + Sy) NextBar = NextBar + Wbar Case "0" 'Draw a narrow bar. g.FillRectangle(color, NextBar, Sy, Nbar + NextBar, my + Sy) NextBar = NextBar + Nbar End Select Next CountY 'Toggle the color (black/white). If color Is pW Then color = pB Else color = pW End If 'Draw intermediate "quiet" bar. g.FillRectangle(color, NextBar, Sy, Qbar + NextBar, my + Sy) NextBar = NextBar + Qbar Next CountX End Function Function MD_BC39(ByVal CharCode As String) As String Dim BC39(90) As String BC39(32) = "011000100" ' space BC39(36) = "010101000" ' $ BC39(37) = "000101010" ' % BC39(42) = "010010100" ' * Start/Stop BC39(43) = "010001010" ' + BC39(45) = "010000101" ' | BC39(46) = "110000100" ' . BC39(47) = "010100010" ' / BC39(48) = "000110100" ' 0 BC39(49) = "100100001" ' 1 BC39(50) = "001100001" ' 2 BC39(51) = "101100000" ' 3 BC39(52) = "000110001" ' 4 BC39(53) = "100110000" ' 5 BC39(54) = "001110000" ' 6 BC39(55) = "000100101" ' 7 BC39(56) = "100100100" ' 8 BC39(57) = "001100100" ' 9 BC39(65) = "100001001" ' A BC39(66) = "001001001" ' B BC39(67) = "101001000" ' C BC39(68) = "000011001" ' D BC39(69) = "100011000" ' E BC39(70) = "001011000" ' F BC39(71) = "000001101" ' G BC39(72) = "100001100" ' H BC39(73) = "001001100" ' I BC39(74) = "000011100" ' J BC39(75) = "100000011" ' K BC39(76) = "001000011" ' L BC39(77) = "101000010" ' M BC39(78) = "000010011" ' N BC39(79) = "100010010" ' O BC39(80) = "001010010" ' P BC39(81) = "000000111" ' Q BC39(82) = "100000110" ' R BC39(83) = "001000110" ' S BC39(84) = "000010110" ' T BC39(85) = "110000001" ' U BC39(86) = "011000001" ' V BC39(87) = "111000000" ' W BC39(88) = "010010001" ' X BC39(89) = "110010000" ' Y BC39(90) = "011010000" ' Z Return BC39(Asc(CharCode)) End Function
Möchte man wie ich die Barcodes in einer ASP.NET-Anwendung ausgeben, kann folgender (schlampiger) Code als kleine Anregung verwendet werden:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim barcode As String = Request("barcode") If Len(barcode) = 0 Then barcode = "" Dim height As String = Request("height") If (Len(height) = 0) Or (height > 768) Then height = 75 Dim width As String = Request("width") If (Len(width) = 0) Or (width > 1024) Then width = 250 Dim plain As Boolean = True Dim xh, xw As Integer If Len(Request("plain")) <> 0 Then If Request("plain") = 1 Then plain = True xw = width xh = height height = height + 15 Else plain = False End If End If Dim bmp As New Bitmap(width, height, PixelFormat.Format24bppRgb) Dim g As Graphics = Graphics.FromImage(bmp) MD_Barcode39(barcode, g, 0, 0, width, height) If plain = True Then Dim dispText As String = barcode Dim dispFont As New Font("arial", 15, FontStyle.Regular, GraphicsUnit.Pixel) g.FillRectangle(Brushes.White, 0, xh, xw, 15) g.DrawString(dispText, dispFont, Brushes.Black, New PointF(0, xh)) End If bmp.Save(Response.OutputStream, ImageFormat.Jpeg) bmp.Dispose() Response.End() End Sub
In die @Page-Direktive muss noch ContentType=”image/jpeg” hinzugefügt werden:
<%@ Page Language="VB" ContentType="image/jpeg" AutoEventWireup="true" CodeFile="barcode.aspx.vb" Inherits="barcode" %>
Aufrufen kann man das Dokument anschließend über den lokalen Webserver oder einen (externen) IIS. Der Aufruf vom Bild oben lautet:
http://localhost:12384/barcode.aspx?barcode=1234567890&width=400&plain=1