zoukankan      html  css  js  c++  java
  • 分享一个C#创建Barcode的DLL

    用于工作需要产生Barcode,随手从网上找了一个DLL(原文地址忘了)

    http://files.cnblogs.com/panchunting/barcode_bin.zip

    使用非常简单,只需添加引用,然后编码如下

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using BarcodeLib;
    
    namespace WebAppBarcode
    {
        public partial class GetBarCodeImage : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string strEncode = Request.QueryString["Code"];
                CreateImage(strEncode);
            }
    
            private void CreateImage(string Code)
            {
                BarcodeLib.Barcode barcode = new BarcodeLib.Barcode()
                {
                    IncludeLabel = true,
                    Alignment = AlignmentPositions.CENTER,
                    Width = 300,
                    Height = 100,
                    RotateFlipType = RotateFlipType.RotateNoneFlipNone,
                    BackColor = Color.White,
                    ForeColor = Color.Black,
                };
    
                System.Drawing.Image img = barcode.Encode(TYPE.CODE128B, Code);
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    Response.ClearContent();
                    Response.ContentType = "image/png";
                    Response.BinaryWrite(ms.ToArray());
                }
            }
        }
    }

    调用后台页面

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebAppBarcode
    {
        public partial class _Default : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                imgBarcode.Src = "~/GetBarCodeImage.aspx?Code=" + this.TextBox1.Text;
            }
        }
    }

    调用前台代码

        <ol class="round">
            <li>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Barcode" OnClick="Button1_Click" />
            </li>
            <li>
                <img id="imgBarcode" runat="server" style="height: 20mm;  100mm;" />
            </li>
        </ol>

    最终效果如下

    多说一句,img的长度要设长一点,尤其当字符比较长的时候,否则可能扫描枪无法扫出

  • 相关阅读:
    SpringCloud系列——SSO 单点登录
    SpringBoot系列——Redis
    基于“formData批量上传的多种实现” 的多图片预览、上传的多种实现
    SpringCloud系列——Bus 消息总线
    SpringCloud系列——Config 配置中心
    SpringCloud系列——Zuul 动态路由
    SpringCloud系列——Ribbon 负载均衡
    SpringCloud系列——Feign 服务调用
    SpringCloud系列——Eureka 服务注册与发现
    ZXing 生成、读取二维码(带logo)
  • 原文地址:https://www.cnblogs.com/panchunting/p/BarCode.html
Copyright © 2011-2022 走看看