C # Web control for barcode generation

Source: Internet
Author: User

Original: Http://www.codeproject.com/Tips/846860/Csharp-Barcode-Generator-Web-Control

in ASP. NET Web pages, a barcode is generated from a Web control in C #.

Brief introduction:

I worked in a small company, and a few days ago someone asked how to generate barcodes on ASP pages. I searched a circle on Google, most of the generation bar code is actually ASP "Page_Load" method through the Response.outputstream to save the barcode picture, this scheme, can only display an identical bar code on the page. But not enough to meet the demand. The page will display at least 2 barcodes, and some text messages will be displayed on the page.

All I gave up this approach and finally found another way to generate barcodes. By using WebControl and IHttpHandler.

Using WebControl and IHttpHandler

Because of the company's needs, I'm using third-party build controls, and you can use zxing as an open source control.

First create a WebControl inheritance htmltextwritertag.img. Add some barcode properties to the image attribute in Src.

 Publicbarcodewebgenerator ():Base(htmltextwritertag.img) {...} protected Override voidAddAttributesToRender (HtmlTextWriter writer) {Base.     AddAttributesToRender (writer); stringHttpHandler ="barcodegeneratehandler.ashx"; Writer. AddAttribute (HTMLTEXTWRITERATTRIBUTE.SRC, HttpHandler+"?"+ This. GetQueryString (),false); }  Private stringgetquerystring () {StringBuilder sb=NewStringBuilder (); Sb. Append ("type="+ This.     Type.tostring ()); Sb. Append ("&text="+ This.     Text.tostring ()); Sb. Append ("&barcodewidth="+ This.     Barcodewidth.tostring ()); Sb. Append ("&barcodeheight="+ This.                Barcodeheight.tostring ()); returnsb.  ToString (); }

Looking at the code above, the SRC attribute adds ashx. Therefore, if the Barcode property changes, then the barcode image will be redrawn through the HttpHandler class. In this example I only added 4 most commonly used properties (barcode type, barcode content, bar code height, barcode width).

The only thing you need to do in IHttpHandler this class is to rewrite the ProcessRequest method, and each request will be processed in this class. Generate barcode images and save in content parameters. When the property or SRC attribute of a barcode control changes, the barcode picture is automatically redrawn. It's simple, isn't it?

 Public voidProcessRequest (HttpContext context) {Try{Constructbarcode (context.        Request);        Updateproperties (); Bitmap BMP=_barcode.        Createbarcode (); Context.        Response.Clear (); Context. Response.ContentType="Image/png"; using(MemoryStream ms =NewMemoryStream ()) {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        { }    } }

Most of the work is done and the barcode generation controls are added to the toolbar on the Web Application page. Then drag and drop 2 bar code control to your page, in the page's CS file in the Page_Load method to set the properties of two controls respectively.

protected voidPage_Load (Objectsender, EventArgs e) {      This. Barcodewebgenerator1.width = -;  This. Barcodewebgenerator1.height = -;  This. Barcodewebgenerator1.text ="ABC123465789ABC";  This. Barcodewebgenerator2.width = -;  This. Barcodewebgenerator2.height = $;  This. Barcodewebgenerator2.type =barcodetype.code128;  This. Barcodewebgenerator2.text ="123456789"; }

In addition, you can redraw the barcode image by dynamically changing the Src property of the barcode Web control by JavaScript (because the control is inherited from image).

As follows:

document.getElementById ('BarcodeWebGenerator1'). src="  Barcodegeneratehandler.ashx? type=qrcode&text=987654321&barcodewidth=200&barcodeheight=200".

Note: The arcodegeneratehandler.ashx file needs to be placed in the same folder as the page page.

This is a short article, hope to be able to help you.

C # Web control for barcode generation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.