需求:

  • Barcode Professional for ASP.NETv3.0以上
  • Microsoft .NET 框架(所有版本)

由于条件的限制,条码显示位置有限,那么就需要懂得规定的区域内显示条码方法。今天将给大家简单介绍如何在规定区域内显示条码。

我们知道在Barcode Professional有GetBarcodeImage()方法,而这种方法可以通过barsAreaSizeInInches参数实现规定区域内条码的显示。现在我们需注意以下三点:

  • 规定区域的尺寸位置(条码宽和高)
  • 规定目标区域的宽(包括条码加上左右Quiet Zones 区域的宽)
  • 规定目标区域的高(条码栏高度)

假定:编码的数据是1234567890,使用128代码,以300DPI呈现,可供条码显示的位置面积是1*0.5英寸.

参考代码:

VB

 1 Private Sub SaveBarcode()
 2 'Create a Barcode Professional object
 3 Dim bcp As New Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional()
 4 'Set the barcode symbology to Code 128
 5 bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128
 6 'Set the value to encode
 7 bcp.Code = "1234567890"
 8 'Barcode dimensions settings
 9 bcp.BarHeight = 1.0F
10 bcp.BarWidth = 0.01F
11 'Resolution
12 Dim dpi As Single = 300.0F
13 'Target size in inches
14 Dim targetArea As New System.Drawing.SizeF(1.0F, 0.5F)
15 'Get the barcode image fitting the target area
16 Dim imgBarcode As System.Drawing.Image = bcp.GetBarcodeImage(dpi, targetArea)
17 'Save it on disk in PNG format
18 imgBarcode.Save("C:\temp\barcode128.png", System.Drawing.Imaging.ImageFormat.Png)
19 imgBarcode.Dispose()
20 End Sub

C#

 1 private void SaveBarcode()
 2 {
 3 //Create a Barcode Professional object
 4 Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional bcp = new Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional();
 5 //Set the barcode symbology to Code 128
 6 bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128;
 7 //Set the value to encode
 8 bcp.Code = "1234567890";
 9 //Barcode dimensions settings
10 bcp.BarHeight = 1.0f;
11 bcp.BarWidth = 0.01f;
12 //Resolution
13 float dpi = 300.0f;
14 //Target size in inches
15 System.Drawing.SizeF targetArea = new System.Drawing.SizeF(1.0f, 0.5f);
16 //Get the barcode image fitting the target area
17 System.Drawing.Image imgBarcode = bcp.GetBarcodeImage(dpi, targetArea);
18 //Save it on disk in PNG format
19 imgBarcode.Save(@"C:\temp\barcode128.png", System.Drawing.Imaging.ImageFormat.Png);
20 imgBarcode.Dispose();
}

需要注意的是目标宽度是整个条码栏宽度加上左右两边的Quiet Zones (QuietZoneWidth属性 ),然而目标区域高度仅仅只是条码栏的高度,可参考下图(前面代码生成)。

figure

如果只想条码布满整个目标区域,需要将QuietZoneWidth属性设置为0(即除去左右两边的边距)DisplayCode属性设置为False(即隐藏可读文本),可参考下列代码

VB

 1 Private Sub SaveBarcode()
 2 'Create a Barcode Professional object
 3 Dim bcp As New Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional()
 4 'Set the barcode symbology to Code 128
 5 bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128
 6 'Set the value to encode
 7 bcp.Code = "1234567890"
 8 'Barcode dimensions settings
 9 bcp.BarHeight = 1.0F
10 bcp.BarWidth = 0.01F
11 bcp.QuietZoneWidth = 0
12 bcp.DisplayCode = False
13 'Resolution
14 Dim dpi As Single = 300.0F
15 'Target size in inches
16 Dim targetArea As New System.Drawing.SizeF(1.0F, 0.5F)
17 'Get the barcode image fitting the target area
18 Dim imgBarcode As System.Drawing.Image = bcp.GetBarcodeImage(dpi, targetArea)
19 'Save it on disk in PNG format
20 imgBarcode.Save("C:\temp\barcode128_full.png", System.Drawing.Imaging.ImageFormat.Png)
21 imgBarcode.Dispose()
22 End Sub

C#

 1 private void SaveBarcode()
 2 {
 3 //Create a Barcode Professional object
 4 Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional bcp = new Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional();
 5 //Set the barcode symbology to Code 128
 6 bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128;
 7 //Set the value to encode
 8 bcp.Code = "1234567890";
 9 //Barcode dimensions settings
10 bcp.BarHeight = 1.0f;
11 bcp.BarWidth = 0.01f;
12 bcp.QuietZoneWidth = 0;
13 bcp.DisplayCode = false;
14 //Resolution
15 float dpi = 300.0f;
16 //Target size in inches
17 SizeF targetArea = new System.Drawing.SizeF(1.0f, 0.5f);
18 //Get the barcode image fitting the target area
19 System.Drawing.Image imgBarcode = bcp.GetBarcodeImage(dpi, targetArea);
20 //Save it on disk in PNG format
21 imgBarcode.Save(@"C:\temp\barcode128_full.png", System.Drawing.Imaging.ImageFormat.Png);
22 imgBarcode.Dispose();
23 }
figure
转载需注明来自慧都控件网