因公司需要用到一維條碼的功能,在網路上搜尋到一篇滿不錯用的
因公司需要用到一維條碼的功能,在網路上搜尋到一篇滿不錯用的
Default.aspx.vb

Protected Sub Button1_ServerClick() Sub Button1_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.ServerClick
Image1.Src = "imga.aspx?mycode=" & Me.TextBox1.Value
End Sub
imga.aspx.vb

Imports ...System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Text
Imports System.Drawing.Text
Imports System.Drawing.Imaging

Partial
Class imga
Inherits System.Web.UI.Page

Private Sub Page_Load() Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在這裡放置使用者程式碼以初始化網頁
Dim mycode As String = Request("mycode")
If Not (mycode = "") Then
Dim 字串, 字元 As String
'字串 = "*-%$*"
字串 = "*" & mycode & "*"
Dim 畫布高, 畫布寬, 筆x, 筆y, 筆寬, i, j As Integer
筆y = 20
筆寬 = 0
畫布高 = 35
畫布寬 = 字串.Length * 13
Dim BMP As Bitmap = New Bitmap(畫布寬, 畫布高, Imaging.PixelFormat.Format32bppPArgb)
Dim G As Graphics = Graphics.FromImage(BMP)
G.TextRenderingHint = TextRenderingHint.AntiAlias
G.Clear(Color.White)
Dim 筆刷1 As Brush = New SolidBrush(Color.White)
G.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
G.FillRectangle(筆刷1, 0, 0, 畫布寬, 畫布高)
For i = 0 To 字串.Length - 1
字元 = barcode(字串.Substring(i, 1).ToUpper)
For j = 0 To 3
If 字元.Substring(j, 1).Equals("0") Then
G.DrawLine(Pens.Black, 筆x, 0, 筆x, 筆y)
Else
G.DrawLine(Pens.Black, 筆x, 0, 筆x, 筆y)
G.DrawLine(Pens.Black, 筆x + 1, 0, 筆x + 1, 筆y)
筆x += 1
End If
筆x += 1
If 字元.Substring(j + 5, 1).Equals("0") Then
G.DrawLine(Pens.White, 筆x, 0, 筆x, 筆y)
Else
G.DrawLine(Pens.White, 筆x, 0, 筆x, 筆y)
G.DrawLine(Pens.White, 筆x + 1, 0, 筆x + 1, 筆y)
筆x += 1
End If
筆x += 1
Next j
If 字元.Substring(4, 1).Equals("0") Then
G.DrawLine(Pens.Black, 筆x, 0, 筆x, 筆y)
Else
G.DrawLine(Pens.Black, 筆x, 0, 筆x, 筆y)
G.DrawLine(Pens.Black, 筆x + 1, 0, 筆x + 1, 筆y)
筆x += 1
End If
筆x += 2
Next i
Dim x As Integer = 0
Dim addx As Integer
addx = 13
G.DrawString("-", _
New Font("Arial", 10, FontStyle.Italic), _
SystemBrushes.WindowText, New PointF(x, 20))
x += addx
For i = 0 To mycode.Length - 1
G.DrawString(mycode.Chars(i), _
New Font("Arial", 10, FontStyle.Italic), _
SystemBrushes.WindowText, New PointF(x, 20))
x = x + addx
Next
G.DrawString("-", _
New Font("Arial", 10, FontStyle.Italic), _
SystemBrushes.WindowText, New PointF(x, 20))
BMP.Save(Response.OutputStream, ImageFormat.Jpeg)
G.Dispose()
BMP.Dispose()
Else
Dim 畫布高, 畫布寬, 筆x, 筆y, 筆寬, i, j As Integer
筆y = 20
筆寬 = 0
畫布高 = 35
畫布寬 = 100
Dim BMP As Bitmap = New Bitmap(畫布寬, 畫布高, Imaging.PixelFormat.Format32bppPArgb)
Dim G As Graphics = Graphics.FromImage(BMP)
G.TextRenderingHint = TextRenderingHint.AntiAlias
G.Clear(Color.White)
G.DrawString("無條碼產生", _
New Font("標楷體", 12, FontStyle.Regular), _
SystemBrushes.WindowText, New PointF(0, 20))
BMP.Save(Response.OutputStream, ImageFormat.Jpeg)
G.Dispose()
BMP.Dispose()
End If
End Sub

Function barcode() barcode(ByVal code As String)
Select Case code
Case 0
code = "001100100"
Case 1
code = "100010100"
Case 2
code = "010010100"
Case 3
code = "110000100"
Case 4
code = "001010100"
Case 5
code = "101000100"
Case 6
code = "011000100"
Case 7
code = "000110100"
Case 8
code = "100100100"
Case 9
code = "010100100"
Case "A"
code = "100010010"
Case "B"
code = "010010010"
Case "C"
code = "110000010"
Case "D"
code = "001010010"
Case "E"
code = "101000010"
Case "F"
code = "011000010"
Case "G"
code = "000110010"
Case "H"
code = "100100010"
Case "I"
code = "010100010"
Case "J"
code = "001100010"
Case "K"
code = "100010001"
Case "L"
code = "010010001"
Case "M"
code = "110000001"
Case "N"
code = "001010001"
Case "O"
code = "101000001"
Case "P"
code = "011000001"
Case "Q"
code = "000110001"
Case "R"
code = "100100001"
Case "S"
code = "010100001"
Case "T"
code = "001100001"
Case "U"
code = "100011000"
Case "V"
code = "010011000"
Case "W"
code = "110001000"
Case "X"
code = "001011000"
Case "Y"
code = "101001000"
Case "Z"
code = "011001000"
Case "*"
code = "001101000"
Case "-"
code = "000111000" '好像變識不出來
Case "%"
code = "100101000" '好像變識不出來
Case "$"
code = "010101000" '好像變識不出來
Case Else
code = "010101000" '都不是就印$
End Select
Return code
End Function
End Class