Search code examples
c#asp.netbarcodebarcode-printingcode39

Barcode Image not getting scanned


I have created Barcodes for labels in my asp.net mvc application. Now I am inserting that image in excel sheet to print labels. The issue is barcode scanner is unable to read barcode.

I am generating Code 39 barcode as below:

string barcodeName = string.Format("{0}{1}", orderItem.StyleNumber + "-" + orderItem.Color + "-" + itemDetail.Size, ".png");
string barcode = orderItem.StyleNumber + "-" + orderItem.Color + "-" + itemDetail.Size;
//Settings for the Image
string TypeFaceName = "IDAutomationHC39M"; // this is the name of font from which your barcode is generated.
string imageLocation = HttpContext.Current.Server.MapPath("~/Bine/Assets/Barcode Images");

//The format of the image file
ImageFormat format = ImageFormat.Png;

string path = Path.Combine(HttpContext.Current.Server.MapPath("~/Bine/Assets/Barcode Images"), barcodeName);


//REFERENCING A FONT 
PrivateFontCollection fnts = new PrivateFontCollection();
fnts.AddFontFile("IDAutomationHC39M.ttf");// this is the name of font from which your barcode is generated.
FontFamily fntfam = new FontFamily(TypeFaceName, fnts);
System.Drawing.Font fnt = new System.Drawing.Font(fntfam, 10);
fnts.AddFontFile("Arial.ttf");
FontFamily fntfam2 = new FontFamily("Arial", fnts);
//DRAWING THE IMAGE  
int w = barcode.Length * 40;
Bitmap bmp = new Bitmap(w, 100);           //Canvas size
Graphics g = Graphics.FromImage(bmp);
// Create the Point and Brushes for the barcode
PointF oPoint = new PointF(2f, 2f);
SolidBrush oBrushWrite = new SolidBrush(Color.Black);
SolidBrush oBrush = new SolidBrush(Color.White);

// Create the actual barcode image
// with a rectangle filled with white color

g.FillRectangle(oBrush, 0, 0, w, 80);
// Put prefix and sufix of an asterisk (*),
// in order to be a valid barcode
g.DrawString("*" + barcode + "*", fnt, oBrushWrite, oPoint);

bmp.Save(path, format); //Saving the Image file
bmp.Dispose(); //Releasing all resources (Image file)

The barcode images are succesfully created and saved in folder. Then I am creating an excel sheet and inserting this images as below:

string imgName = String.Format("{0}{1}", stList[x].Text + "-" + stList[x].Color + "-" + stList[x].Size, ".png");
string path = Path.Combine(HttpContext.Current.Server.MapPath("~/Bine/Assets/Barcode Images"), imgName);
Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)ws.Cells[rw+3, cl];
float Left = (float)((double)oRange.Left);
float Top = (float)((double)oRange.Top);
const float ImageWidth = 200;
const float ImageHeight = 26;

ws.Shapes.AddPicture(path, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageWidth, ImageHeight);

The image too is inserted correctly in excel but when I take print out and scan the barcode its not scanning.

I have checked my scanner and its working fine as I have created similar barcode online from web,took printout and they are scanned perfectly.

The only difference I can make out is in the visual appearance of barcode generated online and my application. The lines printed for online barcode are more solid whereas the the barcode generated from my application has hazy lines. Not sure if this could be the cause.

I have been struggling from last couple of days on this. Please advise.


Solution

  • Your bar code is probably not reading because of image scaling which is making it fuzzy. Instead of inserting images into Excel you should put the bar code text into the cells and set the cell font to your bar code font. This will result in crisp, readable bar codes.