Click here to Skip to main content
15,886,637 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear experts,
I want to implement barcode generation in windows application.So how i can do this.Please help me.Thanks in advance.
Posted
Comments
ZurdoDev 20-Nov-13 10:11am    
Find a 3rd party control that can do it, I would suggest.

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)
 
Share this answer
 
Comments
hrishikesh45 21-Nov-13 10:28am    
Thanks....i want a barcode which start by the character(like A,B,C etc)
Marc Gabrie 21-Nov-13 11:51am    
simple linear barcodes like Code 39, Code 128, etc can encode that. Code 128 is most reliable & compact than Code 39 and can be decode by any simple scanner these days.
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">
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900