Click here to Skip to main content
15,888,287 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am trying to print a barcode in my web page.

string barcode = Label23.Text;
        Bitmap bitmap = new Bitmap(barcode.Length * 40,150);
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            Font ofont = new System.Drawing.Font("IDAutomationHC39M", 20);
            PointF point = new PointF(2f, 2f);
            SolidBrush black = new SolidBrush(Color.Black);
            SolidBrush white = new SolidBrush(Color.White);
            graphics.FillRectangle(white, 0, 0, bitmap.Width, bitmap.Height);
            graphics.DrawString("*"+barcode+"*",ofont,black,point);

        }
        using (MemoryStream ms = new MemoryStream())
        {
            string mypath = Request.PhysicalApplicationPath + "Images\\";
            bitmap.Save(mypath+ "barcode.png",ImageFormat.Png);
            Image2.ImageUrl = "~/Images/barcode.png";


        }


What I have tried:

instead of getting the barcode i am getting the text that i enter.
Posted
Updated 26-May-16 1:48am
Comments
Jared Stroebele 25-May-16 12:43pm    
When I was generating and displaying bar codes I used Brad Barnhill's library he wrote an article about it here Barcode-Image-Generation-Library

1 solution

First you need to check the font "IDAutomationHC39M" working fine on your machine.

Then use below code

Here path is any local path like @c:\Image\

C#
public void CreateBarcode(string data) {
var imgPath = string.Format("{0}{1}.png", path, data);
Bitmap barcode = new Bitmap(1, 1);
Font threeOfNine = new Font("IDAutomationHC39M", 60,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point);
Graphics graphics = Graphics.FromImage(barcode);
SizeF dataSize = graphics.MeasureString(data, threeOfNine);
barcode = new Bitmap(barcode, dataSize.ToSize());
graphics = Graphics.FromImage(barcode);
graphics.Clear(Color.White);
graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
graphics.DrawString(data, threeOfNine, new SolidBrush(Color.Black), 0, 0);
graphics.Flush();
threeOfNine.Dispose();
graphics.Dispose();
barcode.Save(imgPath);
}
 
Share this answer
 
Comments
Bojjaiah 5-Apr-19 1:30am    
Height and width is not working.

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