畫出Code39 BarCode

 Designed by Freepik

報表裡面有Code39 BarCode的時候
如果使用者端沒有安裝對應的字型
會發生無法顯示的狀況。

這裡紀錄一下,如何在使用程式直接畫出Code39 BarCode






  1. 1. 首先在「報表屬性」→「程式碼」新增程式

Public Function MakeBarcodeImage(ByVal datastring As String) As Byte()
  Dim sCode As String = String.Empty
  '清除該頁輸出緩存,設置該頁無緩存
  '將Code39條碼寫入記憶體流,並將其以 "image/Png" 格式輸出
  Dim oStream As New System.IO.MemoryStream()
  Try
    Dim oBmp As System.Drawing.Bitmap = GetCode39(datastring)
    oBmp.Save(oStream, System.Drawing.Imaging.ImageFormat.Png)
    oBmp.Dispose()
    Return oStream.ToArray()
  Finally
    '釋放資源
    oStream.Dispose()
  End Try
End Function
 

Private Function GetCode39(ByVal strSource As String) As System.Drawing.Bitmap
  Dim x As Integer = 5
  '左邊界
  Dim y As Integer = 0
  '上邊界
  Dim WidLength As Integer = 2
  '粗BarCode長度
  Dim NarrowLength As Integer = 1
  '細BarCode長度
  Dim BarCodeHeight As Integer = 18
  'BarCode高度
  Dim intSourceLength As Integer = strSource.Length
  Dim strEncode As String = "010010100"
  '編碼字串 初值為 起始符號 *
  Dim AlphaBet As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"
  'Code39的字母
  'Code39的各字母對應碼

  Dim Code39 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"}
  strSource = strSource.ToUpper()
  '實作圖片
  Dim objBitmap As New System.Drawing.Bitmap(((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2), BarCodeHeight + (y * 2))
  Dim objGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(objBitmap)
  '宣告GDI+繪圖介面
  '填上底色
  objGraphics.FillRectangle(System.Drawing.Brushes.White, 0, 0, objBitmap.Width, objBitmap.Height)

  For i As Integer = 0 To intSourceLength - 1
    '檢查是否有非法字元
    If AlphaBet.IndexOf(strSource(i)) = -1 OrElse strSource(i) = "*"c Then
      objGraphics.DrawString("含有非法字元", System.Drawing.SystemFonts.DefaultFont, System.Drawing.Brushes.Red, x, y)
      Return objBitmap
    End If
    '查表編碼
    strEncode = String.Format("{0}0{1}", strEncode, Code39(AlphaBet.IndexOf(strSource(i))))
  Next

  strEncode = String.Format("{0}0010010100", strEncode)
  '補上結束符號 *
  Dim intEncodeLength As Integer = strEncode.Length
  '編碼後長度
  Dim intBarWidth As Integer

  For i As Integer = 0 To intEncodeLength - 1
    '依碼畫出Code39 BarCode
    If strEncode(i) = "1"c Then
      intBarWidth = WidLength
    Else
      intBarWidth = NarrowLength
    End If
    If i Mod 2 = 0 Then
      objGraphics.FillRectangle(System.Drawing.Brushes.Black, x, y, intBarWidth, BarCodeHeight)
    Else
      objGraphics.FillRectangle(System.Drawing.Brushes.White, x, y, intBarWidth, BarCodeHeight)
    End If
    x = x + intBarWidth
  Next
  Return objBitmap
End Function

  1. 2.. 在欲顯示BarCode的地方插入影像物件,然後設定如下圖:

沒有留言:

張貼留言

技術提供:Blogger.