条形码图像未被扫描

时间:2014-05-30 04:33:25

标签: c# asp.net barcode barcode-printing code39

我在asp.net mvc应用程序中为标签创建了条形码。现在我将该图像插入Excel工作表中以打印标签。问题是条形码扫描仪无法读取条形码。

我正在生成Code 39条形码,如下所示:

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)

条形码图像已成功创建并保存在文件夹中。然后我创建一个excel表并插入如下图像:

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);

图像也正确插入excel中,但是当我打印出来并扫描条形码时它不会扫描。

我检查过我的扫描仪并且工作正常,因为我在网上创建了类似的条形码,打印输出并且扫描完美。

唯一的区别在于在线生成的条形码和我的应用程序的视觉外观。为在线条形码打印的线条更加坚固,而从我的应用程序生成的条形码具有朦胧的线条。不确定这是不是原因。

过去几天我一直在努力奋斗。请指教。

1 个答案:

答案 0 :(得分:0)

您的条形码可能无法读取,因为图像缩放使其模糊。您应该将条形码文本放入单元格,并将单元格字体设置为条形码字体,而不是将图像插入Excel。这将产生清晰,可读的条形码。