c#產生條碼的web控制項

來源:互聯網
上載者:User

標籤:

原文:http://www.codeproject.com/Tips/846860/Csharp-Barcode-Generator-Web-Control

在asp.net的web頁用c#的web控制項產生條碼。

簡介:

我在一個小公司工作,幾天前有人詢問在asp頁面產生條碼的方法。我在Google上搜了一圈,大多數產生條碼實在asp的”page_load”方法裡通過Response.OutputStream來儲存條碼圖片,這種方案,只能在頁面上顯示一個相同的條碼。但是不能滿足需求。該頁面至少要顯示2個以上的條碼,而且頁面上還要顯示一些文字資訊。

所有我放棄了這種方法,最終找到了另一個方法來產生條碼。通過使用WebControl 和IHttpHandler.

使用WebControl 和IHttpHandler

因為公司的需求,我使用的是第三方產生控制項,你也可以使用zxing這種開原始檔控制。

首先建立一個webcontrol繼承HtmlTextWriterTag.Img。在圖片屬性裡src 裡添加一些條碼屬性。

public BarcodeWebGenerator()          : base(HtmlTextWriterTag.Img)  {     ...  }  protected override void AddAttributesToRender(HtmlTextWriter writer)  {     base.AddAttributesToRender(writer);     string httpHandler = "BarcodeGenerateHandler.ashx";     writer.AddAttribute(HtmlTextWriterAttribute.Src, httpHandler + "?" +      this.GetQueryString(), false);  }  private string GetQueryString()  {     StringBuilder sb = new StringBuilder();     sb.Append("Type=" + this.Type.ToString());     sb.Append("&Text=" + this.Text.ToString());     sb.Append("&BarcodeWidth=" + this.BarcodeWidth.ToString());     sb.Append("&BarcodeHeight=" + this.BarcodeHeight.ToString());                return sb.ToString();  }

看上面的代碼,src屬性裡添加了ashx。因此如果條碼屬性發生改變,那麼條碼圖片會通過HttpHandler類重繪。這個例子中我只添加了4個最常用的屬性(條碼類型,條碼內容,條碼高度,條碼寬度)。

在IHttpHandler這個類你唯一需要做的一件事就是重寫ProcessRequest 方法,每一個請求都會在這個類中被處理。產生條碼圖片和儲存在內容參數裡。當條碼控制項的屬性或src屬性發生變化,條碼圖片會自動重繪。很簡單,不是嗎?

public void ProcessRequest(HttpContext context) {    try    {        constructBarcode(context.Request);        updateProperties();        Bitmap bmp = _barcode.CreateBarcode();        context.Response.Clear();        context.Response.ContentType = "image/png";        using (MemoryStream ms = new MemoryStream())        {            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);            //context.Response.ClearContent();                                context.Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length);        }            bmp.Dispose();            context.Response.Flush();        }        catch        { }    } }

大部分工作已經完成了,在web應用程式頁把條碼產生控制項加到工具條裡。然後拖放2個條碼控制項到你的頁面,在page的cs檔案的page_load方法裡分別給兩個控制項設定屬性。

protected void Page_Load(object sender, EventArgs e) {     this.BarcodeWebGenerator1.Width = 300;     this.BarcodeWebGenerator1.Height = 300;     this.BarcodeWebGenerator1.Text = "abc123465789ABC";     this.BarcodeWebGenerator2.Width = 300;     this.BarcodeWebGenerator2.Height = 200;     this.BarcodeWebGenerator2.Type = BarCodeType.Code128;     this.BarcodeWebGenerator2.Text = "123456789"; }

除此之外,你可以通過javascript(因為該控制項是從image繼承而來)來動態改變條碼web控制項的src屬性來重繪條碼圖片。

如下:

document.getElementById(‘BarcodeWebGenerator1‘).src="BarcodeGenerateHandler.ashx?Type=QRCode&Text=987654321&BarcodeWidth=200&BarcodeHeight=200".

注意:arcodeGenerateHandler.ashx檔案需要和page頁放在同一檔案夾下。

這是一個簡短的文章,希望能夠對你有協助。

c#產生條碼的web控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.