Barcode Professional for ASP.NET使用教程:条码图像保存到数据库或者XML文件

Barcode Professional里面有个专门的获取条码图像的方法,该方法可以用数组字节来表示条码图像生成,因此我们可以调用这种方法来将条码图像保存到数据库中或者XML文件。

在下面的示例中,我们将创建一个ASP.NET的Web应用程序,该应用可以通过DataSet对象把条码图像保存到XML文件。

步骤:

  • 打开.NET开发工具,如Visual Studio .NET 并创建一个新的ASP.NET Web应用
  • 拖放下列控件到设计界面:
    • Barcode Professional控件
    • TextBox控件
    • Button控件
    • Panel控件并加入到Literal 控件里
  • 设置 Barcode Professional's Symbology 属性 128码
  • 设置Panel's Visible属性 False
  • 双击按钮控件并将下列代码写入Button1_Click 事件程序

VB

 1 'Set the value to encode
 2 BarcodeProfessional1.Code = TextBox1.Text
 3 'Create a DataSet and save the barcode image
 4 Dim ds As DataSet = New DataSet("MyDataSet")
 5 Dim dt As DataTable = New DataTable("MyTable")
 6 ds.Tables.Add(dt)
 7 'Create a column to hold the barcode image
 8 Dim dc As DataColumn = New DataColumn("BarcodeImage", GetType(Byte()))
 9 dt.Columns.Add(dc)
10 'Create a new row
11 Dim dr As DataRow = dt.NewRow()
12 'Save the barcode image
13 dr("BarcodeImage") = BarcodeProfessional1.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Gif)
14 dt.Rows.Add(dr)
15 'Show the DataSet content
16 Literal1.Text = Server.HtmlEncode(ds.GetXml())
17 Panel1.Visible = True

C#

 1 //Set the value to encode
 2 BarcodeProfessional1.Code = TextBox1.Text;
 3 //Create a DataSet and save the barcode image
 4 DataSet ds = new DataSet("MyDataSet");
 5 DataTable dt = new DataTable("MyTable");
 6 ds.Tables.Add(dt);
 7 //Create a column to hold the barcode image
 8 DataColumn dc = new DataColumn("BarcodeImage", typeof(byte[]));
 9 dt.Columns.Add(dc);
10 //Create a new row
11 DataRow dr = dt.NewRow();
12 //Save the barcode image
13 dr["BarcodeImage"] = BarcodeProfessional1.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Gif);
14 dt.Rows.Add(dr);
15 //Show the DataSet content
16 Literal1.Text = Server.HtmlEncode(ds.GetXml());
17 Panel1.Visible = true;

  

运行创建的ASP.NET Web应用程序,你将看到一下输出

Barcode Professional for ASP.NET使用教程:条码图像保存到数据库或者XML文件
Barcode Professional for ASP.NET使用教程:条码图像保存到数据库或者XML文件