为IDAutomationHC39M创建2位数的条形码,但不扫描

时间:2017-09-05 14:09:46

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

我正在尝试为数字50或51生成IDAutomationHC39M生成的条形码但是在扫描时我无法获得详细信息编号。

同样,我可以为14757创建条形码,在这种情况下,我可以正确扫描条形码。

我应该如何扫描2位数生成的条形码。

在下面的代码中是否有一些我做错了。

public void generateBarcode(int id)
    {
        if (plBarCode != null)
        {
            string barCode = "";

            barCode = Convert.ToString(id);
            System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
            using (Bitmap bitMap = new Bitmap(barCode.Length * 70, 70))
            {
                using (Graphics graphics = Graphics.FromImage(bitMap))
                {
                    Font oFont = new Font("IDAutomationHC39M", 30);
                    PointF point = new PointF(2f, 2f);
                    SolidBrush blackBrush = new SolidBrush(Color.Black);
                    SolidBrush whiteBrush = new SolidBrush(Color.White);
                    graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);                       
                    int w = barCode.Length * 40;                    
                    Graphics oGraphics = Graphics.FromImage(bitMap);                    
                    PointF oPoint = new PointF(2f, 2f);
                    SolidBrush oBrushWrite = new SolidBrush(Color.Black);
                    SolidBrush oBrush = new SolidBrush(Color.White);         
                    oGraphics.FillRectangle(oBrush, 0, 0, w, 100);                       
                    oGraphics.DrawString("*" + barCode + "*", oFont, oBrushWrite, oPoint);

                }
                using (MemoryStream ms = new MemoryStream())
                {
                    bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] byteImage = ms.ToArray();

                    Convert.ToBase64String(byteImage);
                    imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                }
                plBarCode.Controls.Add(imgBarCode);
            }
        }

    }

生成下面带有数字56的条形码,并且在扫描时仍然从传递的字符串中删除*而不返回数据56.

enter image description here

3 个答案:

答案 0 :(得分:1)

看起来你有6个角色。两个'*'字符开始和结束条形码。

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/AQD/AAD/AAAAAAAAAAAAAAABAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAATUoCA+Jw8kAAAAAASUVORK5CYII=" width="10" height="40"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/Af//AAABAAAAAAAAAAAAAAD/AAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAajkDAr7LFAwAAAAASUVORK5CYII=" width="10" height="40"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/AQD/AAAAAAAAAAAAAP8AAAABAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAARVICA/z3WM0AAAAASUVORK5CYII=" width="10" height="40"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/Af//AAABAAAAAAAAAAAAAAD/AAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAajkDAr7LFAwAAAAASUVORK5CYII=" width="10" height="40"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/AQD/AAD/AAAAAAAAAAAAAAABAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAATUoCA+Jw8kAAAAAASUVORK5CYII=" width="10" height="40"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/ASL/AADeAAAA/gAAAAIAAAD+AAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAh6QEAZKdo54AAAAASUVORK5CYII=" width="10" height="40"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/AQD/AAD/AAAAAQAAAP8AAAABAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAak4DA7VAmk0AAAAASUVORK5CYII=" width="10" height="40"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/Af//AAAAAAAAAAAAAAEAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAUUoCA+juoggAAAAASUVORK5CYII=" width="10" height="40"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/AQD/AAD/AAAAAQAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAATVICAxOyzrMAAAAASUVORK5CYII=" width="10" height="40"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/Af//AAABAAAA/wAAAAEAAAD/AAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAizUEAhrxcbgAAAAASUVORK5CYII=" width="10" height="40"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/Af//AAAAAAAAAQAAAP8AAAABAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAbkoDA3iPVH4AAAAASUVORK5CYII=" width="10" height="40"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/AQD/AAAAAAAA/wAAAAEAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAASVICA62zYxMAAAAASUVORK5CYII=" width="10" height="40"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/AQD/AAD/AAAAAQAAAP8AAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAajUDAvnk9E4AAAAASUVORK5CYII=" width="10" height="40">

当我插入50时,我得到了这个。看起来你需要在'DrawString'方法中删除你在barCode之前和之后添加的两个星号。

这是我添加的图片。

enter image description here

答案 1 :(得分:1)

使用此修改后的代码。

public void generateBarcode(string id)
{
                int w = id.Length * 55;

                // Create a bitmap object of the width that we calculated and height of 100
                Bitmap oBitmap = new Bitmap(w, 100);
                // then create a Graphic object for the bitmap we just created.
                Graphics oGraphics = Graphics.FromImage(oBitmap);
                // Now create a Font object for the Barcode Font
                // (in this case the IDAutomationHC39M) of 18 point size
                Font oFont = new Font("IDAutomationHC39M", 18);
                // Let's 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);
                // Now lets create the actual barcode image
                // with a rectangle filled with white color
                oGraphics.FillRectangle(oBrush, 0, 0, w, 100);
                // We have to put prefix and sufix of an asterisk (*),
                // in order to be a valid barcode
                oGraphics.DrawString("*" + id + "*", oFont, oBrushWrite, oPoint);
                // Then we send the Graphics with the actual barcode
                System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();

                using (System.IO.FileStream fs = System.IO.File.Open(Server.MapPath("~/img/barcodes/") + id + ".jpg", FileMode.Create))
                {
                    oBitmap.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);

                }
                oBitmap.Dispose();
                imgbarcode.ImageUrl = "~/img/barcodes/"+id+".jpg";
}

答案 2 :(得分:0)

您可能想尝试一下:

  protected void btnGenerate_Click(object sender, EventArgs e)

    {

        string barCode = txtCode.Text;
        //testbar.Text = txtCode.Text;
        System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();

        using (Bitmap bitMap = new Bitmap(barCode.Length * 28, 90))
        {

            using (Graphics graphics = Graphics.FromImage(bitMap))
            {
                var fonts = new PrivateFontCollection();
                fonts.AddFontFile(Server.MapPath("~/font/IDAutomationHC39M.ttf"));
                Font oFont = new Font((FontFamily)fonts.Families[0], 17f);
                //Font oFont = new Font("IDAutomationHC39M", 10,FontStyle.Regular,GraphicsUnit.Pixel);
                PointF point = new PointF(2f, 2f);
                SolidBrush blackBrush = new SolidBrush(Color.Black);
                SolidBrush whiteBrush = new SolidBrush(Color.White);
                graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
                graphics.DrawString("*" + barCode + "*", oFont , blackBrush, point);
            }
            using (MemoryStream ms = new MemoryStream())
            {
                bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] byteImage = ms.ToArray();

                Convert.ToBase64String(byteImage);
                imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
            }
            plBarCode.Controls.Add(imgBarCode);
        }
    }