zoukankan      html  css  js  c++  java
  • 译:c#生成条码的web控件

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

    在asp.net的web页用c#的web控件生成条码。

    web-demo1

    简介:

    我在一个小公司工作,几天前有人询问在asp页面生成条码的方法。我在谷歌上搜了一圈,大多数生成条码实在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页放在同一文件夹下。

    这是一个简短的文章,希望能够对你有帮助。

  • 相关阅读:
    SQL Server 2012 同步数据
    SQL md5 加密
    构建BootStrap
    js toFixed 真正四舍五入
    Angular BootStrap 登录页面
    Gitblit 安装使用
    js 函数
    git push error HTTP code = 413
    react-native 插件汇总
    js 监听组合键盘事件
  • 原文地址:https://www.cnblogs.com/yplong/p/4266205.html
Copyright © 2011-2022 走看看