Creating BarCode using VB.Net. or Barcode and Vb.Net.
In this project we used font which is added to font at run time. It is barcode font it convert entered text in form of Barcode value. There is file name "rmad " this must be copied into Debug folder of project so that font could be at installed run time. After generation of Barcode equivalent value and image would be created in Debug folder automatically containing Barcode value. You could download whole project as well as rmad file if need it explicitly otherwise it is already in project Debug folder.
I am using visual studio 2010. I have converted this project compatible to framework version 2.0 but still if it doesn’t work. Then just follow the steps and create your own project. This code is acceptable to all version of .Net.
Step 1: Create project and add 4 controls on form. That are : one button name=button1, two label (no matter that what is name of second label but you must name one label with name=Label1 (this label is must for result if you are using code directly)) and one textbox with name=textbox1.
Step 2: Copy this while code to your class and also copy namespace used. Copy carefully because if you would copy it in improper manner then this code won’t be working for you.
Step 3: Copy ‘rmad’ file to Debug folder. ‘rmad’ file is available for download here.
Solution code:
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class Form1
Dim PFC As Drawing.Text.PrivateFontCollection
Dim NewFont_FF As Drawing.FontFamily
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objBitmap As New Bitmap(100, 50)
Dim objGraphics As Graphics = Graphics.FromImage(objBitmap)
Dim objPoint As New PointF(5.0F, 5.0F)
objGraphics.FillRectangle(Brushes.White, 0, 0, Width, Height)
objGraphics.DrawString(Me.TextBox1.Text, CreateFont(Application.StartupPath & "\rmad", 20, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, objPoint)
objBitmap.Save("Result.JPG", ImageFormat.Jpeg)
Me.Label1.Font = CreateFont(Application.StartupPath & "\rmad", 20, FontStyle.Regular, GraphicsUnit.Point)
Me.Label1.Text = Me.TextBox1.Text
End Sub
Private Function CreateFont(ByVal name As String, ByVal size As Single, ByVal style As Drawing.FontStyle, ByVal unit As Drawing.GraphicsUnit) As Drawing.Font
PFC = New Drawing.Text.PrivateFontCollection
PFC.AddFontFile(name)
NewFont_FF = PFC.Families(0)
Return New Drawing.Font(NewFont_FF, size, style, unit)
End Function
End Class
Explanation:
Imports System.Drawing and Imports System.Drawing.Imaging namespace is used for generation of image with barcode value.
Imports System namespace is used to import system control. In this for installing font dynamically at run time and writing image file this namespace is required.
Why installing font dynamically ?
Because it won’t lead in revealing secret to your client that this is so simple to generate. Second and main reason is this; it won’t result in error because whole job is of packaging properly no job required by client of installing font.
Output:
Barform view.
Result.jpg.