建立報表(ReportViewer) - 加入BarCode
安裝了字型,在自己電腦開發完以後,
很高興的release程式,結果....因為使用者端沒有字型,
條碼根本無法顯示阿!
所以谷歌了一下,有網友分享以下的方法,非常好用!^^
經過一些測試,要來紀錄一下!
原始方法出處
- 新增一個ReportViewer報表(RDLC),點選報表→報表屬性
- 須先參考組件,參考→加入→選擇組件加入。
經過測試以後,若您安裝了.NetFrameWork 4.5 或 4.5.2版,
新增的兩個System.Drawing v4.0.0.0都可以選用參考喔。
- 同樣是報表屬性視窗,切換到程式碼,將以下的程式碼複製貼上!。
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
- 返回報表,於工具箱中拖曳一個「影像」物件到報表上,
並按滑鼠右鍵選擇「影像屬性」。
屬性請參照以下的圖案。 使用此欄位的設定中,請填入欲轉成BarCode欄位名稱。
就是這樣,很簡單的,使用者也不需要額外安裝字型了!
沒有留言:
張貼留言