Barcode Generation using C#

In this article, I am trying to give you a sample code for generating barcode image by using C#.

We can see in market for all products they are using barcode only. So the barcode is common one. When we try to programming with old language for barcode is very difficult. We needed to go third party component. But in .Net framework it is very easy to generate barcode image.

Barcode font

There is lot of free barcode fonts available in internet. We can search and download it for generating the barcode image. Here is the font which is I am using “Barcodefont.ttf”. We need to download this font and install in your fonts folder which is located in control panel.

For generating barcode image, we need to add the namespaces “System.Drawing, System.Drawing.Imaging” and the file management namespaces.

Sample Code

using System.Drawing.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

public string dirPath = "";

dirPath = Environment.CurrentDirectory;

private void GenerateBarCode()
{
try
{
string path = dirPath + "\\" + DateTime.Now.ToFileTimeUtc().ToString() + ".jpg";
PrivateFontCollection fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile(dirPath + "\\BarcodeFont.ttf");
FontFamily fontFamily = new FontFamily("barcode font", fontCollection);
Font font = new Font(fontFamily, 40);

Bitmap bmp = new Bitmap(pbBarcodeImage.Width, pbBarcodeImage.Height);
Graphics graphics = Graphics.FromImage(bmp);
graphics.Clear(Color.White);
SizeF sizeF = graphics.MeasureString(txtEnter.Text.Trim(), font);
Brush brush = new SolidBrush(Color.Black);
graphics.DrawString(txtEnter.Text.Trim(), font, brush, 10, 10);
bmp.Save(path, ImageFormat.Jpeg);
bmp.Dispose();
pbBarcodeImage.Image = new Bitmap(path);
}
catch
{ }
}

Hence we are generating the barcode by using the above code.

...S.VinothkumaR.

Inserting Bulk Data in SQL Server from Text file

There is a simple query to do this bulk insert in to sql server from a text file. The thing is that we have to keep a fully formatted text file with appropriate data’s which are to be inserted.

Here, we have a table to be inserted is,

The query for creating table,

CREATE TABLE Employee
(
FName varchar (100) NOT NULL,
LName varchar (100) NOT NULL,
Email varchar (100) NOT NULL
)

Then we have a text file with the bulk data’s like as follows,

vinoth,kumar,itvinoth83@gmail.com
emp1FName,emp1LName,emp1@company.com
emp2FName,emp2LName,emp2@company.com
emp3FName,emp3LName,emp3@company.com
emp4FName,emp4LName,emp4@company.com
emp5FName,emp5LName,emp5@company.com
emp6FName,emp6LName,emp6@company.com
emp7FName,emp7LName,emp7@company.com
emp8FName,emp8LName,emp8@company.com
emp9FName,emp9LName,emp9@company.com
emp10FName,emp10LName,emp10@company.com
emp11FName,emp11LName,emp11@company.com
emp12FName,emp12LName,emp12@company.com

Now the query for inserting bulk data is,

BULK INSERT Employee FROM 'c:\bulktext.txt' WITH (FIELDTERMINATOR = ',')

Now you can see the data's inserted in table by the select query as follows,

select * from Employee

The following image is being explained the above matters.





...S.VinothkumaR.