C# 未扫描条形码图像

C# 未扫描条形码图像,c#,asp.net,barcode,barcode-printing,code39,C#,Asp.net,Barcode,Barcode Printing,Code39,我已经在asp.net mvc应用程序中为标签创建了条形码。现在我在excel工作表中插入该图像以打印标签。问题是条形码扫描仪无法读取条形码 我正在生成代码39条形码,如下所示: string barcodeName = string.Format("{0}{1}", orderItem.StyleNumber + "-" + orderItem.Color + "-" + itemDetail.Size, ".png"); string barcode = orderItem.StyleNum

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

我正在生成代码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)
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工作表并插入以下图像:

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)
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中,但当我打印出来并扫描条形码时,它不会扫描

我已经检查了我的扫描仪,它工作正常,因为我已经从网上创建了类似的条形码,打印出来,它们被完美地扫描了

我能看出的唯一区别是在线生成的条形码和我的应用程序的视觉外观。在线条形码打印的线条更加坚实,而我的应用程序生成的条形码线条模糊。不确定这是否是原因


这几天来我一直在努力。请告知。

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

为什么要将图像插入excel?只需将条形码文本yourbarcodevalue放入单元格,并将单元格字体设置为条形码字体。图像可能正在缩放,或者有什么东西使其模糊且无法读取。非常感谢。我会按建议去试的。你说得对。我的图像被缩小了,因为我没有提供足够的宽度。现在开始工作了。无论如何,你的建议似乎是更好的解决办法。我会试试看这是否也有效。再次感谢。请选择我的答案,如果它有助于你解决你的问题。当然可以。我只是想落实你的建议。我正在寻找通过asp.net应用程序更改特定单元格字体的方法。我知道如何为整个工作表设置字体,但在这种情况下,我只需要为1个单元格设置字体。您需要获取单元格、列、行或单元格范围的引用,并为该引用设置字体属性。你可以为此单独发布一个问题,如果你发布一些代码,我可能会帮你解决这个问题。