Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

i m working on project in which client need to genrate barcode image for there product.i don't have any idea about it.if any budy know that how to genrate barcode image & save it into database as image or print it.

Thanks & Regards
Hussain
Posted

C#
public void DrawingBarcode(string strNumber)
        {
            PrivateFontCollection fonts = new PrivateFontCollection();  
            //39码
            strNumber = "*" + strNumber + "*";
            fonts.AddFontFile(HttpContext.Current.Server.MapPath(".") + "/BarcodeFonts/FREE3OF9.TTF");
            FontFamily ff = new FontFamily("Free 3 of 9", fonts);         
            Font font = new Font(ff, 12);

            //设置图片大小
             System.Drawing.Image img =  new Bitmap(1, 1);
            Graphics g = Graphics.FromImage(img);
            SizeF fontSize = g.MeasureString(strNumber, font);
            int intWidth = Convert.ToInt32(fontSize.Width);
            int intHeight = Convert.ToInt32(fontSize.Height);
            g.Dispose();
            img.Dispose();

            img = new Bitmap(intWidth, intHeight);
            g = Graphics.FromImage(img);
            g.Clear(Color.White);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.DrawString(strNumber, font, Brushes.Black, 0, 0);
            MemoryStream stream = new MemoryStream();
            img.Save(stream, ImageFormat.Jpeg);
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ContentType = "image/Jpeg";
            HttpContext.Current.Response.BinaryWrite(stream.ToArray());
            g.Dispose();
            img.Dispose();
        }
 
Share this answer
 
v2
 
Share this answer
 
v2
Hussain,
If you plan on using a library for generating the Barcode, you can use either a free library or a commercial library. There are a lot of articles on Codeproject for both cases.
Please see the following article for a example on a free generation library:
Barcodes in ASP.NET applications
Please see the following article for a example on a commercial barcode library:
How to Read Barcodes from Images using LEADTOOLS
The above example is for reading Barcodes but the library can be used to generate Barcode.
 
Share this answer
 
v2
Hi:
It's convenient to generate barcodes in .net using C#.

1,Unzip BizCode Generator for .NET Ultimate package;
2,Add BusinessRefinery.Barcode.Web.dll to you ASP.NET project folder;
(The DLL will be copied to the bin directory automatically)
3,Then BusinessRefinery.Barcode.Web.dll is added on your ASP.NET project

Here is a sample coding:

using BusinessRefinery.Barcode;

Linear barcode = new Linear();
barcode.Symbology = Symbology.CODE39;
barcode.Code = "0123456789";
barcode.DisplayStartStopChar = true;
barcode.Resolution = 104;
barcode.Rotate = Rotate.Rotate180;
barcode.Format = ImageFormat.Gif;
barcode.drawBarcode2ImageFile("c:/code-39-csharp.gif");

You can cutomize it based on your needs.
 
Share this answer
 
[Spam content has been removed]
 
Share this answer
 
v2

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