为什么不显示我的条形码?

时间:2016-03-07 12:10:57

标签: vb.net

我尝试显示带有字符串的条形码,我有这样的字符串:SO-123456 但不会用条形码显示我的图像。

我的代码是:

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Partial Class VB
    Inherits System.Web.UI.Page
    Protected Sub btnGenerate_Click(sender As Object, e As EventArgs)
        Dim barCode As String = txtCode.Text
        Dim imgBarCode As New System.Web.UI.WebControls.Image()
        Using bitMap As New Bitmap(barCode.Length * 40, 80)
            Using graphics__1 As Graphics = Graphics.FromImage(bitMap)
                Dim oFont As New Font("IDAutomationHC39M", 16)
                Dim point As New PointF(2.0F, 2.0F)
                Dim blackBrush As New SolidBrush(Color.Black)
                Dim whiteBrush As New SolidBrush(Color.White)
                graphics__1.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height)
                graphics__1.DrawString("*" & barCode & "*", oFont, blackBrush, point)
            End Using
            Using ms As New MemoryStream()
                bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
                Dim byteImage As Byte() = ms.ToArray()

                Convert.ToBase64String(byteImage)
                imgBarCode.ImageUrl = "data:image/png;base64," & Convert.ToBase64String(byteImage)
            End Using
            plBarCode.Controls.Add(imgBarCode)
        End Using
    End Sub
End Class

图形页面是:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="VB.aspx.vb" Inherits="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
    <asp:Button ID="btnGenerate" runat="server" Text="Generate" 
        onclick="btnGenerate_Click" />
    <hr />
    <asp:PlaceHolder ID="plBarCode" runat="server" />
    </form>
</body>
</html>

如何正确获取IDAutomationHC39M,我可以放入文件夹并尝试打开该代码栏吗?我不知道

我想要那样: enter image description here

但我表示:

enter image description here

1 个答案:

答案 0 :(得分:1)

Dim oFont As New Font("IDAutomationHC39M", 16)

需要成为

Dim privateFonts As New System.Drawing.Text.PrivateFontCollection()
privateFonts.AddFontFile("C:\Documents and Settings\somefont.ttf")
Dim oFont As New System.Drawing.Font(privateFonts.Families(0), 12)