【问题标题】:To generate barcode through web application通过 Web 应用程序生成条形码
【发布时间】:2015-10-16 06:55:15
【问题描述】:

我想通过我的网络应用程序生成条形码。我试图谷歌找出源代码来实现它,但我得到的代码是通过 Windows 应用程序生成条形码。但我希望代码在我的 Web 应用程序中包含相同的功能。 Web应用程序的代码应该是什么? web应用中的picturebox应该用什么替换?

Windows 应用程序代码:

using (MemoryStream ms = new MemoryStream())
{
    bitmap.Save(ms, ImageFormat.Png);
    pictureBox1.Image = bitmap;
    pictureBox1.Height = bitmap.Height;
    pictureBox1.Width = bitmap.Width;
}

生成条形码的代码:

private void button1_Click(object sender, EventArgs e)
{
    string barcode = textBox1.Text;

    Bitmap bitmap = new Bitmap(barcode.Length + 40, 150);

    using (Graphics graphics = Graphics.FromImage(bitmap))
    {
        Font ofont = new System.Drawing.Font("Free 3 of 9 Extended", 40);
        PointF point = new PointF(2f, 2f);
        SolidBrush black = new SolidBrush(Color.Black);
        SolidBrush white = new SolidBrush(Color.White);
        graphics.FillRectangle(white, 0, 0, bitmap.Width, bitmap.Height);
        graphics.DrawString("*" + barcode + "*", ofont, black, point);
    }
}

【问题讨论】:

    标签: c# asp.net web-applications barcode


    【解决方案1】:

    您不需要任何 c# 代码。这是一个示例html页面

    <html>
    <style>
        @font-face {
            font-family: barcode;
            src: url(free3of9.ttf);
        }
    </style>
    
    <body>
        ABC
        <div style='font-family:barcode;font-size:32px;'>123456789</div>
        DEF
    </body>
    
    </html>
    

    这是输出:

    【讨论】:

      【解决方案2】:

      您可以使用.net WebSocket 服务器SuperWebSocket 将图像数据发送到Web 客户端。这是一个简单的例子:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using SuperSocket.SocketBase;
      using SuperSocket.SocketBase.Config;
      using SuperSocket.SocketBase.Logging;
      using SuperSocket.SocketBase.Protocol;
      using SuperSocket.SocketEngine;
      
      namespace SuperSocket.QuickStart.TelnetServer
      {
          class Program
          {
              static void Main(string[] args)
              {
                  Console.WriteLine("Press any key to start the server!");
      
                  Console.ReadKey();
                  Console.WriteLine();
      
                  var appServer = new AppServer();
      
                  //Setup the appServer
                  if (!appServer.Setup(2012)) //Setup with listening port
                  {
                      Console.WriteLine("Failed to setup!");
                      Console.ReadKey();
                      return;
                  }
      
                  appServer.NewSessionConnected += new SessionHandler<AppSession>(appServer_NewSessionConnected);
      
                  Console.WriteLine();
      
                  //Try to start the appServer
                  if (!appServer.Start())
                  {
                      Console.WriteLine("Failed to start!");
                      Console.ReadKey();
                      return;
                  }
      
                  Console.WriteLine("The server started successfully, press key 'q' to stop it!");
      
                  while (Console.ReadKey().KeyChar != 'q')
                  {
                      Console.WriteLine();
                      continue;
                  }
      
                  Console.WriteLine();
                  //Stop the appServer
                  appServer.Stop();
      
                  Console.WriteLine("The server was stopped!");
              }
      
              static void appServer_NewSessionConnected(AppSession session)
              {
                  session.Send("Welcome to SuperSocket Telnet Server");
              }
          }
      }
      

      您可以使用 session.send() 发送图像数据。 然后创建一个 div 元素并在 JavaScript 中绘制图像:

      var ws = new WebSocket("ws://127.0.0.1:2012/");  
      ws.onmessage = function (evt) {     
          var bytes = new Uint8Array(evt.data);
          var data = "";
          var len = bytes.byteLength;
          for (var i = 0; i < len; ++i) {
              data += String.fromCharCode(bytes[i]);
          }
          var img = document.getElementById("image");
          img.src = "data:image/png;base64,"+window.btoa(data);   
      }; 
      

      【讨论】:

        猜你喜欢
        • 2012-05-08
        • 1970-01-01
        • 2013-08-26
        • 1970-01-01
        • 2012-08-31
        • 2011-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多