根据用户输入填充BarCode

问题描述 投票:1回答:1

我试图成功打印基于用户输入的BarCode,但BarCode未正确填充。图像高度和宽度随用户输入而增加/减少。

请参阅下面的参考图像.enter image description here

对于这些,我试过下面的代码。

public void fun()
    {
        string barCode = txtCode.Text;
        System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
        using (Bitmap bitMap = new Bitmap(Convert.ToInt32(txtW.Text), Convert.ToInt32(txtH.Text)))
        {
            using (Graphics graphics = Graphics.FromImage(bitMap))
            {
                Font oFont = new Font(@"C:\Users\bojjaiah.thoti\Downloads\IDAutomationCode39\IDAutomation.com Free Code 39 Font\IDAutomationHC39M.ttf", 16);
                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);
        }
    }

请根据用户输入建议填写条形码。

c# asp.net barcode
1个回答
1
投票

ID Automation Code 39字体是一种字体,因此据我所知,您必须设置字体大小来调整比例(因此字体实例化结束时'16'需要调整为用户输入)。通过独立调整垂直和水平刻度,我不确定字体是否可以很好地缩放。

您可能想要做的是提示用户输入宽度(以像素或英寸为单位)及其字符串,并根据字符串中的字符数计算要使用的字体大小。然后计算条形码的高度,如果该高度大于用户高度要求,则在条形码顶部绘制一个白色矩形,以便在正确高度处剪掉条形。

var buttonGen = document.getElementById("btnGen");
buttonGen.onclick = function () {
  var x = document.getElementById("textIn").value;
  var fs;

  // Change the font-size style to match the drop down
  fs = document.getElementsByTagName("option")[document.getElementById("selList").selectedIndex].value;
  document.getElementById("test").style.fontSize = fs  + 'px';
  
  document.getElementById("test").innerHTML =
    '*' + // Start Code B
    x + // The originally typed string
    '*'; // Stop Code
}
<html>
  <head>
  <link rel="stylesheet" media="screen, print" href="https://fontlibrary.org/face/idautomationhc39m-code-39-barcode" type="text/css"/>
    <style>
      td, th {
        text-align: center;
        padding: 6px;
      }
      .ss {
        font-family: "IDAHC39M Code 39 Barcode";
        font-size: 24px;
      }
    </style>
  </head>
  <body>
    &nbsp;Font Size:&nbsp;
    <select id="selList">
      <option value="12">12px</option>
      <option value="14">14px</option>
      <option value="16">16px</option>
      <option value="18">18px</option>
      <option value="20">20px</option>
      <option value="24" selected>24px</option>
      <option value="30">30px</option>
      <option value="36">36px</option>
      <option value="42">42px</option>
      <option value="48">48px</option>
      <option value="54">54px</option>
      <option value="60">60px</option>
      <option value="66">66px</option>
      <option value="72">72px</option>
      <option value="78">78px</option>
      <option value="84">84px</option>
      <option value="90">90px</option>
      <option value="96">96px</option>
    </select>&nbsp;

    <input type="text" id="textIn" value="12587"></input>
    <input type="button" id="btnGen" value="Generate Code 39" tabindex=4/>
    <div id="check"></div><br /><span id="test" class="ss">*12587*</span><br />
    <p>This is a demonstration of use of the Free ID Automation 39 Font.</p>
  </body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.