0

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.

user3647327
  • 31
  • 2
  • 11
  • Why insert the image into excel? Just put the barcode text *yourbarcodevalue* in the cell and set the cell font to the barcode font. The image is probably being scaled or something making it fuzzy and unreadable. – Shane Wealti Jun 02 '14 at 14:17
  • Thanks a lot. I will try as suggested. You pointed it right. My image was being scaled down as I wasn't providing enough width. Its working now. Anyways your suggestion seem to be better solution. Will try and see if this works too. Thanks again. – user3647327 Jun 03 '14 at 04:05
  • Please select my answer if it helped you solve your problem. – Shane Wealti Jun 03 '14 at 20:22

1 Answers1

0

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.

Shane Wealti
  • 2,252
  • 3
  • 19
  • 33
  • Sure will do. I am just trying to implement your suggestion. I am finding way to change particular cell font through my asp.net application. I know how to set font for entire sheet but in this case I need to set this font for just 1 cell. – user3647327 Jun 04 '14 at 04:59
  • You need to get a reference to the cell, column, row, or range of cells and set the font property for that reference. You can post a separate question for that and I can probably help you out with it if you post some of your code. – Shane Wealti Jun 04 '14 at 12:37