在Vb.net 2008中实施条形码 [英] Implementation of Barcode In Vb.net 2008

查看:67
本文介绍了在Vb.net 2008中实施条形码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家,

我想在Windows应用程序中实现条形码生成。所以我该怎么做。请帮助我。谢谢。

Dear experts,
I want to implement barcode generation in windows application.So how i can do this.Please help me.Thanks in advance.

推荐答案

参考以下文章。



1. 用于Code 39标准的高级条码生成系统 [ ^ ]

2. 使用vb.net制作条形码 [ ^ ]

3. GenCode128 - Code128条码生成器 [ ^ ]

4. 条码图像生成库 [ ^ ]

5. 使用C#创建EAN-13条形码 [ ^ ]
Refer following articles.

1. Advanced Barcode Generation System for Code 39 Standards[^]
2. Make Barcode with vb.net[^]
3. GenCode128 - A Code128 Barcode Generator[^]
4. Barcode Image Generation Library[^]
5. Creating EAN-13 Barcodes with C#[^]


您需要哪种条形码?那里有很多种条形码!您可以在CodeProject上找到一些关于条形码生成的开源代码,或者去寻找其他一些商业工具(警告!有很多条形码工具,但只有少数是条形码标准的真正专家,只需通过联系他们的支持来试试它们根据之前的经验,重要的是要知道产品背后的人会在事情没有达到预期的情况下正确地帮助你。)以下是我的建议:



< a href =http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library>条形码图像生成库 [ ^ ](开源)

http://zxingnet.codeplex.com/ [ ^ ](开源)

用于.NET的条码SDK [ ^ ](商业)

。NET Windows窗体条形码控件 [ ^ ](商业)
Which barcode do you need? There are many types of barcodes out there! You can go for some open source code about barcode generation available here at CodeProject or go for some other commercial tools (Warning! There are plenty of Barcode tools BUT just few of them are genuine specialists on barcode standards, just try them by contacting their support. Per previous experience, it's important to know that the people behind the product will assist you correctly when things do not go as expected.) Here are my suggestions:

Barcode Image Generation Library[^] (open source)
http://zxingnet.codeplex.com/[^] (open source)
Barcode SDK for .NET[^] (commercial)
.NET Windows Forms Barcode Control[^] (Commercial)


以下是步骤:

1将条形码库dll安装到ASP.NET web项目,你可以google it.Find dll从下载的试用包中集成到你的Visual Studio ASP.NET web项目中。

2将ASP.NET条码控件库添加到Toolbox.Right点击工具箱,选择选择项目...,然后单击浏览...以找到DLL。

3将qrcode.aspx和qrcode.aspx.cs复制到您的Web项目文件夹。

如果要创建线性条形码或其他2d条形码,请复制相关信息将文件添加到项目文件夹中。

4将QRCodeWebForm拖放到ASPX Web表单并运行项目。然后,在ASP.NET项目中可以看到高质量的条形码图像,你可以使用VB.NET自由调整其属性。请查看QR Code属性设置的详细信息。



您想在vb.net中生成什么样的条形码?如何获取code39 vb.net中的条形码作为示例?









_encoding.Add(+,bWbwbWbWb)

_encoding.Add(0,bwbWBwBwb)

_encoding.Add(1,BwbWbwbwB)

_encoding.Add (2,bwBWbwbwB)

_encoding.Add(3,BwBWbwbwb)

_encoding.Add(4,bwbWBwbwB)

_encoding.Add(5,BwbWBwbwb)

_encoding.Add(6,bwBWBwbwb)

_encoding.Add (7,bwbWbwBwB)

_encoding.Add(8,BwbWbwBwb)

_encoding.Add(9,bwBWbwBwb)

_encoding.Add(A,BwbwbWbwB)

_encoding.Add(B,bwBwbWbwB)

_encoding.Add (C,BwBwbWb wb)

_encoding.Add(D,bwbwBWbwB)

_encoding.Add(E,BwbwBWbwb)

_encoding.Add(F,bwBwBWbwb)

_encoding.Add(G,bwbwbWBwB)

_encoding.Add(H, BwbwbWBwb)

_encoding.Add(我,bwBwbWBwb)

_encoding.Add(J,bwbwBWBwb)

_encoding.Add(K,BwbwbwbWB)

_encoding.Add(L,bwBwbwbWB)

_encoding.Add(M, BwBwbwbWb)

_encoding.Add(N,bwbwBwbWB)

_encoding.Add(O,BwbwBwbWb)

_encoding.Add(P,bwBwBwbWb)

_encoding.Add(Q,bwbwbwBWB)

_encoding.Add(R, BwbwbwBWb)

_encoding.Add(S,bwBwbwBWb)

_encoding.Add(T,bwbwBwBWb)

_encoding.Add(U,BWbwbwbwB)

_encoding.Add(V,bWBwbwbwB)

_encoding.Add(W, BWBwbwbwb)

_encoding.Add(X,bWbwBwbwB)

_encoding.Add(Y,BWbwBwbwb)

_encoding.Add(Z,bWBwBwbwb)

End Sub



受保护的子Page_Load(ByVal sender As Object ,ByVal e As System.EventArgs)Handles Me.Load

BarcodeCode39()

Dim barcode As String = String.Empty

If Not IsNothing (请求(条形码))AndAlso Not(请求(条形码)。长度= 0)然后

条形码=请求(条形码)

Response.ContentType =image / png

Response.AddHeader(Content-Disposition,String.Format(attachment; filename = barcode_ {0} .png,条形码))



'TODO:根据字符串的长度,确定图像的宽度

GenerateBarcodeImage(250,140,条形码).WriteTo(Response.OutputStream)

结束如果

结束子



受保护的函数getBCSymbolColor(ByVal符号为字符串)As System.Drawing.Brush

getBCSymbolColor = Brushes.Black

如果symbol =W或符号=w然后

getBCSymbolColor = Brushes.White

结束如果

结束功能



受保护的函数getBCSymbolWidth(ByVal symbol As String)As short

getBCSymbolWidth = _narrowBarWidth

如果symbol =B或symbol =W那么

getBCSymbolWidth = _wideBarWidth

结束如果

结束功能



受保护的可覆盖函数GenerateBarcodeImage(ByVal imageWidth As Short,ByVal imageHeight As Short,ByVal Code As Str ing)作为MemoryStream

'创建一个新的位图

Dim b As New Bitmap(imageWidth,imageHeight,Imaging.PixelFormat.Format32bppArgb)



'创建画布上画画

昏暗画布作为新矩形(0,0,imageWidth,imageHeight)



'绘制白色背景

Dim g As Graphics = Graphics.FromImage(b)

g.FillRectangle(Brushes.White,0,0,imageWidth,imageHeight)



'在底部写下未经修改的代码

'TODO:真正居中此文本

Dim textBrush As新的SolidBrush(Color.Black)

g.DrawString(代码,新字体(Courier New,12),textBrush,100,110)



'代码必须用星号包围才能使其成为有效的Code39条形码

Dim UseCode As String = String.Format({0} {1} {0},* ,代码)



'10点开始绘图,10

Dim XPosition As Short = 10

Dim YPosition As Short = 10



Dim invalidcharacter As Boolean = False

Dim CurrentSymbol As String = String.Empty



对于j为短= 0到CShort(UseCode.Length - 1)

CurrentSymbol = UseCode.Substring(j,1)

'检查符号是否可以使用

如果不是IsNothing(_encoding(CurrentSymbol))那么

Dim EncodedSymbol As String = _encoding(CurrentSymbol).ToString



For i Short = 0到CShort(EncodedSymbol.Length - 1)

Dim CurrentCode As String = EncodedSymbol.Substring(i,1)

g.FillRectangle(getBCSymbolColor(CurrentCode),XPosition,YPosition,getBCSymbolWidth(CurrentCode),_ barHeight)

XPosition = XPosition + getBCSymbolWidth(CurrentCode)

Next



'在每个书面完整符号后,我们需要一个空格(窄的宽度)

g.FillRectangle(getBCSymbolColor(w),XPosition, YPos ition,getBCSymbolWidth(w),_ barHeight)

XPosition = XPosition + getBCSymbolWidth(w)

Else

invalidCharacter = True

结束如果

下一页



'找到无效字符时错误处理

如果invalidCharacter然后

g.FillRectangle(Brushes.White,0,0,imageWidth,imageHeight)

g.DrawString(找到无效字符,新字体(Courier)新的,8,textBrush,0,0)

g.DrawString(无条码生成,新字体(Courier New,8),textBrush,0,10)

g.DrawString(输入为:,新字体(Courier New,8),textBrush,0,30)

g.DrawString(代码,新字体( Courier New,8),textBrush,0,40)

结束如果



'将图像写入内存流

Dim ms As New MemoryStream



Dim encodingParams As New EncoderParameters

encodingParams.Param(0)= New EncoderPa rameter(Encoder.Quality,100)



Dim encodingInfo As ImageCodecInfo = FindCodecInfo(PNG)



b.Save(ms,encodingInfo,encodingParams)



'处理我们不再需要的对象

g.Dispose ()

b.Dispose()



返回ms

结束功能



受保护的可覆盖函数FindCodecInfo(ByVal codec As String)As ImageCodecInfo

Dim encoders As ImageCodecInfo()= ImageCodecInfo.GetImageEncoders

For each e作为ImageCodecInfo在编码器中

如果e.FormatDescription.Equals(编解码器)然后返回e

下一页

不返回

结束功能

结束班>
These are the steps :
1 Install a barcode library dll to ASP.NET web project,you can google it.Find dll from downloaded trial package and integrate it into your Visual Studio ASP.NET web project.
2 Add Barcode Control Library for ASP.NET to Toolbox.Right click Toolbox, select Choose Items..., and click Browse... to locate dll.
3 Copy qrcode.aspx and qrcode.aspx.cs to your web project folder.
If you want to create linear barcodes or other 2d barcodes, please copy corresponding files to your project folder.
4 Drag and drop QRCodeWebForm to your ASPX web forms and run the project.Then, a high-quality barcode image will be visible on your ASP.NET project and you can freely adjust its properties using VB.NET. Please see details from QR Code property settings.

What kind of barcode do you want to generate in vb.net?How about taking code39 barcode in vb.net as an example?




_encoding.Add("+", "bWbwbWbWb")
_encoding.Add("0", "bwbWBwBwb")
_encoding.Add("1", "BwbWbwbwB")
_encoding.Add("2", "bwBWbwbwB")
_encoding.Add("3", "BwBWbwbwb")
_encoding.Add("4", "bwbWBwbwB")
_encoding.Add("5", "BwbWBwbwb")
_encoding.Add("6", "bwBWBwbwb")
_encoding.Add("7", "bwbWbwBwB")
_encoding.Add("8", "BwbWbwBwb")
_encoding.Add("9", "bwBWbwBwb")
_encoding.Add("A", "BwbwbWbwB")
_encoding.Add("B", "bwBwbWbwB")
_encoding.Add("C", "BwBwbWbwb")
_encoding.Add("D", "bwbwBWbwB")
_encoding.Add("E", "BwbwBWbwb")
_encoding.Add("F", "bwBwBWbwb")
_encoding.Add("G", "bwbwbWBwB")
_encoding.Add("H", "BwbwbWBwb")
_encoding.Add("I", "bwBwbWBwb")
_encoding.Add("J", "bwbwBWBwb")
_encoding.Add("K", "BwbwbwbWB")
_encoding.Add("L", "bwBwbwbWB")
_encoding.Add("M", "BwBwbwbWb")
_encoding.Add("N", "bwbwBwbWB")
_encoding.Add("O", "BwbwBwbWb")
_encoding.Add("P", "bwBwBwbWb")
_encoding.Add("Q", "bwbwbwBWB")
_encoding.Add("R", "BwbwbwBWb")
_encoding.Add("S", "bwBwbwBWb")
_encoding.Add("T", "bwbwBwBWb")
_encoding.Add("U", "BWbwbwbwB")
_encoding.Add("V", "bWBwbwbwB")
_encoding.Add("W", "BWBwbwbwb")
_encoding.Add("X", "bWbwBwbwB")
_encoding.Add("Y", "BWbwBwbwb")
_encoding.Add("Z", "bWBwBwbwb")
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BarcodeCode39()
Dim barcode As String = String.Empty
If Not IsNothing(Request("barcode")) AndAlso Not (Request("barcode").Length = 0) Then
barcode = Request("barcode")
Response.ContentType = "image/png"
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=barcode_{0}.png", barcode))

'TODO: Depending on the length of the string, determine how wide the image will be
GenerateBarcodeImage(250, 140, barcode).WriteTo(Response.OutputStream)
End If
End Sub

Protected Function getBCSymbolColor(ByVal symbol As String) As System.Drawing.Brush
getBCSymbolColor = Brushes.Black
If symbol = "W" Or symbol = "w" Then
getBCSymbolColor = Brushes.White
End If
End Function

Protected Function getBCSymbolWidth(ByVal symbol As String) As Short
getBCSymbolWidth = _narrowBarWidth
If symbol = "B" Or symbol = "W" Then
getBCSymbolWidth = _wideBarWidth
End If
End Function

Protected Overridable Function GenerateBarcodeImage(ByVal imageWidth As Short, ByVal imageHeight As Short, ByVal Code As String) As MemoryStream
'create a new bitmap
Dim b As New Bitmap(imageWidth, imageHeight, Imaging.PixelFormat.Format32bppArgb)

'create a canvas to paint on
Dim canvas As New Rectangle(0, 0, imageWidth, imageHeight)

'draw a white background
Dim g As Graphics = Graphics.FromImage(b)
g.FillRectangle(Brushes.White, 0, 0, imageWidth, imageHeight)

'write the unaltered code at the bottom
'TODO: truely center this text
Dim textBrush As New SolidBrush(Color.Black)
g.DrawString(Code, New Font("Courier New", 12), textBrush, 100, 110)

'Code has to be surrounded by asterisks to make it a valid Code39 barcode
Dim UseCode As String = String.Format("{0}{1}{0}", "*", Code)

'Start drawing at 10, 10
Dim XPosition As Short = 10
Dim YPosition As Short = 10

Dim invalidCharacter As Boolean = False
Dim CurrentSymbol As String = String.Empty

For j As Short = 0 To CShort(UseCode.Length - 1)
CurrentSymbol = UseCode.Substring(j, 1)
'check if symbol can be used
If Not IsNothing(_encoding(CurrentSymbol)) Then
Dim EncodedSymbol As String = _encoding(CurrentSymbol).ToString

For i As Short = 0 To CShort(EncodedSymbol.Length - 1)
Dim CurrentCode As String = EncodedSymbol.Substring(i, 1)
g.FillRectangle(getBCSymbolColor(CurrentCode), XPosition, YPosition, getBCSymbolWidth(CurrentCode), _barHeight)
XPosition = XPosition + getBCSymbolWidth(CurrentCode)
Next

'After each written full symbol we need a whitespace (narrow width)
g.FillRectangle(getBCSymbolColor("w"), XPosition, YPosition, getBCSymbolWidth("w"), _barHeight)
XPosition = XPosition + getBCSymbolWidth("w")
Else
invalidCharacter = True
End If
Next

'errorhandling when an invalidcharacter is found
If invalidCharacter Then
g.FillRectangle(Brushes.White, 0, 0, imageWidth, imageHeight)
g.DrawString("Invalid characters found,", New Font("Courier New", 8), textBrush, 0, 0)
g.DrawString("no barcode generated", New Font("Courier New", 8), textBrush, 0, 10)
g.DrawString("Input was: ", New Font("Courier New", 8), textBrush, 0, 30)
g.DrawString(Code, New Font("Courier New", 8), textBrush, 0, 40)
End If

'write the image into a memorystream
Dim ms As New MemoryStream

Dim encodingParams As New EncoderParameters
encodingParams.Param(0) = New EncoderParameter(Encoder.Quality, 100)

Dim encodingInfo As ImageCodecInfo = FindCodecInfo("PNG")

b.Save(ms, encodingInfo, encodingParams)

'dispose of the object we won't need any more
g.Dispose()
b.Dispose()

Return ms
End Function

Protected Overridable Function FindCodecInfo(ByVal codec As String) As ImageCodecInfo
Dim encoders As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders
For Each e As ImageCodecInfo In encoders
If e.FormatDescription.Equals(codec) Then Return e
Next
Return Nothing
End Function
End Class">


这篇关于在Vb.net 2008中实施条形码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆