如何在vb.net中生成Code39条形码

前端之家收集整理的这篇文章主要介绍了如何在vb.net中生成Code39条形码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想从我的应用程序创建Code39编码的条形码.

我知道我可以使用这种字体,但是我不想像在服务器上注册字体那样,我已经有了一些不好的经历.

在提出这个问题之后,我提出的一个例子就是答案

这是我目前的codebehind,有很多评论
  1. Option Explicit On
  2. Option Strict On
  3.  
  4. Imports System.Drawing
  5. Imports System.Drawing.Imaging
  6. Imports System.Drawing.Bitmap
  7. Imports System.Drawing.Graphics
  8. Imports System.IO
  9.  
  10. Partial Public Class Barcode
  11. Inherits System.Web.UI.Page
  12. 'Sebastiaan Janssen - 20081001 - TINT-30584
  13. 'Most of the code is based on this example:
  14. 'http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/04/25/writing-code-39-barcodes-with-javascript.aspx-generation.aspx
  15. 'With a bit of this thrown in:
  16. 'http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/03/24/code-39-barcode
  17.  
  18. Private _encoding As Hashtable = New Hashtable
  19. Private Const _wideBarWidth As Short = 8
  20. Private Const _narrowBarWidth As Short = 2
  21. Private Const _barHeight As Short = 100
  22.  
  23. Sub BarcodeCode39()
  24. _encoding.Add("*","bWbwBwBwb")
  25. _encoding.Add("-","bWbwbwBwB")
  26. _encoding.Add("$","bWbWbWbwb")
  27. _encoding.Add("%","bwbWbWbWb")
  28. _encoding.Add(" ","bWBwbwBwb")
  29. _encoding.Add(".","BWbwbwBwb")
  30. _encoding.Add("/","bWbWbwbWb")
  31. _encoding.Add("+","bWbwbWbWb")
  32. _encoding.Add("0","bwbWBwBwb")
  33. _encoding.Add("1","BwbWbwbwB")
  34. _encoding.Add("2","bwBWbwbwB")
  35. _encoding.Add("3","BwBWbwbwb")
  36. _encoding.Add("4","bwbWBwbwB")
  37. _encoding.Add("5","BwbWBwbwb")
  38. _encoding.Add("6","bwBWBwbwb")
  39. _encoding.Add("7","bwbWbwBwB")
  40. _encoding.Add("8","BwbWbwBwb")
  41. _encoding.Add("9","bwBWbwBwb")
  42. _encoding.Add("A","BwbwbWbwB")
  43. _encoding.Add("B","bwBwbWbwB")
  44. _encoding.Add("C","BwBwbWbwb")
  45. _encoding.Add("D","bwbwBWbwB")
  46. _encoding.Add("E","BwbwBWbwb")
  47. _encoding.Add("F","bwBwBWbwb")
  48. _encoding.Add("G","bwbwbWBwB")
  49. _encoding.Add("H","BwbwbWBwb")
  50. _encoding.Add("I","bwBwbWBwb")
  51. _encoding.Add("J","bwbwBWBwb")
  52. _encoding.Add("K","BwbwbwbWB")
  53. _encoding.Add("L","bwBwbwbWB")
  54. _encoding.Add("M","BwBwbwbWb")
  55. _encoding.Add("N","bwbwBwbWB")
  56. _encoding.Add("O","BwbwBwbWb")
  57. _encoding.Add("P","bwBwBwbWb")
  58. _encoding.Add("Q","bwbwbwBWB")
  59. _encoding.Add("R","BwbwbwBWb")
  60. _encoding.Add("S","bwBwbwBWb")
  61. _encoding.Add("T","bwbwBwBWb")
  62. _encoding.Add("U","BWbwbwbwB")
  63. _encoding.Add("V","bWBwbwbwB")
  64. _encoding.Add("W","BWBwbwbwb")
  65. _encoding.Add("X","bWbwBwbwB")
  66. _encoding.Add("Y","BWbwBwbwb")
  67. _encoding.Add("Z","bWBwBwbwb")
  68. End Sub
  69.  
  70. Protected Sub Page_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load
  71. BarcodeCode39()
  72. Dim barcode As String = String.Empty
  73. If Not IsNothing(Request("barcode")) AndAlso Not (Request("barcode").Length = 0) Then
  74. barcode = Request("barcode")
  75. Response.ContentType = "image/png"
  76. Response.AddHeader("Content-Disposition",String.Format("attachment; filename=barcode_{0}.png",barcode))
  77.  
  78. 'TODO: Depending on the length of the string,determine how wide the image will be
  79. GenerateBarcodeImage(250,140,barcode).WriteTo(Response.OutputStream)
  80. End If
  81. End Sub
  82.  
  83. Protected Function getBCSymbolColor(ByVal symbol As String) As System.Drawing.Brush
  84. getBCSymbolColor = Brushes.Black
  85. If symbol = "W" Or symbol = "w" Then
  86. getBCSymbolColor = Brushes.White
  87. End If
  88. End Function
  89.  
  90. Protected Function getBCSymbolWidth(ByVal symbol As String) As Short
  91. getBCSymbolWidth = _narrowBarWidth
  92. If symbol = "B" Or symbol = "W" Then
  93. getBCSymbolWidth = _wideBarWidth
  94. End If
  95. End Function
  96.  
  97. Protected Overridable Function GenerateBarcodeImage(ByVal imageWidth As Short,ByVal imageHeight As Short,ByVal Code As String) As MemoryStream
  98. 'create a new bitmap
  99. Dim b As New Bitmap(imageWidth,imageHeight,Imaging.PixelFormat.Format32bppArgb)
  100.  
  101. 'create a canvas to paint on
  102. Dim canvas As New Rectangle(0,imageWidth,imageHeight)
  103.  
  104. 'draw a white background
  105. Dim g As Graphics = Graphics.FromImage(b)
  106. g.FillRectangle(Brushes.White,imageHeight)
  107.  
  108. 'write the unaltered code at the bottom
  109. 'TODO: truely center this text
  110. Dim textBrush As New SolidBrush(Color.Black)
  111. g.DrawString(Code,New Font("Courier New",12),textBrush,100,110)
  112.  
  113. 'Code has to be surrounded by asterisks to make it a valid Code39 barcode
  114. Dim UseCode As String = String.Format("{0}{1}{0}","*",Code)
  115.  
  116. 'Start drawing at 10,10
  117. Dim XPosition As Short = 10
  118. Dim YPosition As Short = 10
  119.  
  120. Dim invalidCharacter As Boolean = False
  121. Dim CurrentSymbol As String = String.Empty
  122.  
  123. For j As Short = 0 To CShort(UseCode.Length - 1)
  124. CurrentSymbol = UseCode.Substring(j,1)
  125. 'check if symbol can be used
  126. If Not IsNothing(_encoding(CurrentSymbol)) Then
  127. Dim EncodedSymbol As String = _encoding(CurrentSymbol).ToString
  128.  
  129. For i As Short = 0 To CShort(EncodedSymbol.Length - 1)
  130. Dim CurrentCode As String = EncodedSymbol.Substring(i,1)
  131. g.FillRectangle(getBCSymbolColor(CurrentCode),XPosition,YPosition,getBCSymbolWidth(CurrentCode),_barHeight)
  132. XPosition = XPosition + getBCSymbolWidth(CurrentCode)
  133. Next
  134.  
  135. 'After each written full symbol we need a whitespace (narrow width)
  136. g.FillRectangle(getBCSymbolColor("w"),getBCSymbolWidth("w"),_barHeight)
  137. XPosition = XPosition + getBCSymbolWidth("w")
  138. Else
  139. invalidCharacter = True
  140. End If
  141. Next
  142.  
  143. 'errorhandling when an invalidcharacter is found
  144. If invalidCharacter Then
  145. g.FillRectangle(Brushes.White,imageHeight)
  146. g.DrawString("Invalid characters found,",8),0)
  147. g.DrawString("no barcode generated",10)
  148. g.DrawString("Input was: ",30)
  149. g.DrawString(Code,40)
  150. End If
  151.  
  152. 'write the image into a memorystream
  153. Dim ms As New MemoryStream
  154.  
  155. Dim encodingParams As New EncoderParameters
  156. encodingParams.Param(0) = New EncoderParameter(Encoder.Quality,100)
  157.  
  158. Dim encodingInfo As ImageCodecInfo = FindCodecInfo("PNG")
  159.  
  160. b.Save(ms,encodingInfo,encodingParams)
  161.  
  162. 'dispose of the object we won't need any more
  163. g.Dispose()
  164. b.Dispose()
  165.  
  166. Return ms
  167. End Function
  168.  
  169. Protected Overridable Function FindCodecInfo(ByVal codec As String) As ImageCodecInfo
  170. Dim encoders As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders
  171. For Each e As ImageCodecInfo In encoders
  172. If e.FormatDescription.Equals(codec) Then Return e
  173. Next
  174. Return Nothing
  175. End Function
  176. End Class

猜你在找的VB相关文章