ASP.net(c#)產生條碼

來源:互聯網
上載者:User

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(bar_code("www.sosuo8.com", 100, 1, 2));
}
public string bar_code(object str, int ch, int cw, int type_code)
{
//str:輸入的字串;ch:要顯示條碼的高度;cw:要顯示條碼的寬度;type_code:代碼類型
string strTmp = str.ToString();
string code = strTmp;
// ToLower()將string轉化成小寫形式的副本,返回是使用指定地區的性的大小寫規則。
strTmp = strTmp.ToLower();
int height = ch;
int width = cw;

//將傳入的參數進行轉化。
strTmp = strTmp.Replace("0", "_|_|__||_||_|"); ;
strTmp = strTmp.Replace("1", "_||_|__|_|_||");
strTmp = strTmp.Replace("2", "_|_||__|_|_||");
strTmp = strTmp.Replace("3", "_||_||__|_|_|");
strTmp = strTmp.Replace("4", "_|_|__||_|_||");
strTmp = strTmp.Replace("5", "_||_|__||_|_|");
strTmp = strTmp.Replace("7", "_|_|__|_||_||");
strTmp = strTmp.Replace("6", "_|_||__||_|_|");
strTmp = strTmp.Replace("8", "_||_|__|_||_|");
strTmp = strTmp.Replace("9", "_|_||__|_||_|");
strTmp = strTmp.Replace("a", "_||_|_|__|_||");
strTmp = strTmp.Replace("b", "_|_||_|__|_||");
strTmp = strTmp.Replace("c", "_||_||_|__|_|");
strTmp = strTmp.Replace("d", "_|_|_||__|_||");
strTmp = strTmp.Replace("e", "_||_|_||__|_|");
strTmp = strTmp.Replace("f", "_|_||_||__|_|");
strTmp = strTmp.Replace("g", "_|_|_|__||_||");
strTmp = strTmp.Replace("h", "_||_|_|__||_|");
strTmp = strTmp.Replace("i", "_|_||_|__||_|");
strTmp = strTmp.Replace("j", "_|_|_||__||_|");
strTmp = strTmp.Replace("k", "_||_|_|_|__||");
strTmp = strTmp.Replace("l", "_|_||_|_|__||");
strTmp = strTmp.Replace("m", "_||_||_|_|__|");
strTmp = strTmp.Replace("n", "_|_|_||_|__||");
strTmp = strTmp.Replace("o", "_||_|_||_|__|");
strTmp = strTmp.Replace("p", "_|_||_||_|__|");
strTmp = strTmp.Replace("r", "_||_|_|_||__|");
strTmp = strTmp.Replace("q", "_|_|_|_||__||");
strTmp = strTmp.Replace("s", "_|_||_|_||__|");
strTmp = strTmp.Replace("t", "_|_|_||_||__|");
strTmp = strTmp.Replace("u", "_||__|_|_|_||");
strTmp = strTmp.Replace("v", "_|__||_|_|_||");
strTmp = strTmp.Replace("w", "_||__||_|_|_|");
strTmp = strTmp.Replace("x", "_|__|_||_|_||");
strTmp = strTmp.Replace("y", "_||__|_||_|_|");
strTmp = strTmp.Replace("z", "_|__||_||_|_|");
strTmp = strTmp.Replace("-", "_|__|_|_||_||");
strTmp = strTmp.Replace("*", "_|__|_||_||_|");
strTmp = strTmp.Replace("/", "_|__|__|_|__|");
strTmp = strTmp.Replace("%", "_|_|__|__|__|");
strTmp = strTmp.Replace("+", "_|__|_|__|__|");
strTmp = strTmp.Replace(".", "_||__|_|_||_|");
strTmp = strTmp.Replace("_", "<span style='height:" + height + ";width:" + width + ";background:#FFFFFF;'></span>");
strTmp = strTmp.Replace("|", "<span style='height:" + height + ";width:" + width + ";background:#000000;'></span>");

if (type_code == 1)
{
return strTmp + "<BR>" + code;
}
else
{
return strTmp;
}
}
}

方法二:
using System.Drawing;

public void CreateCodeLogo(string code)
    {

        long len = code.Length;
        string lastString = "";
        char[] list = new char[len + 1];

        list = code.ToCharArray();

        for (int i = 0; i < list.Length; i++)
        {
            lastString += this.ConvertToBinaryString(list[i].ToString());
        }

        char[] numList = new char[lastString.Length + 1];
        numList = lastString.ToCharArray();

        Bitmap image = new Bitmap(200, 140);
        Graphics g = Graphics.FromImage(image);

        g.Clear(Color.White);

        Pen penBlack = new Pen(Color.FromArgb(255, 0, 0, 0), 2.5F);
        Pen penWhite = new Pen(Color.White, 2.5F);

        int j = 0;

        for (float k = 10; j < numList.Length; k += 2F, j++)
        {
            if (numList[j].ToString() == "1")
            {
                g.DrawLine(penBlack, k, 10, k, 110);

            }
            else
            {
                g.DrawLine(penWhite, k, 10, k, 110);
            }

            if (j % 4 == 0)
            {
                g.DrawString(list[j / 4].ToString(), new System.Drawing.Font("Courier New", 12), new SolidBrush(Color.Red), k, 112);
            }
        }
        image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
    }

    //將字串數值轉換為二進位字串數值
    public string ConvertToBinaryString(string buf)
    {
        int[] temp = new int[20];
        string binary;
        int val = 0, i = 0, j;

        //先將字元轉化為十進位數
        try
        {
            val = Convert.ToInt32(buf);
        }
        catch
        {
            val = 0;
        }

        if (val == 0)
        {
            return ("0000");
        }

        i = 0;
        while (val != 0)
        {
            temp[i++] = val % 2;
            val /= 2;
        }

        binary = "";

        for (j = 0; j <= i - 1; j++)
        {
            binary += (char)(temp[i - j - 1] + 48);
        }

        if (binary.Length < 4)   //如果小於4位左邊補零
        {
            int len = 4 - binary.Length;
            string str = "";

            while (len > 0)
            {
                str += "0";
                len--;
            }

            binary = str + binary;
        }

        return (binary);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        CreateCodeLogo(TextBox1.Text);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.