分享一個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; width: 100mm;" />
        </li>
    </ol>

最終效果如下

多說一句,img的長度要設長一點,尤其當字符比較長的時候,否則可能掃描槍無法掃出


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM