• C#生成条形码


    转自:http://www.cnblogs.com/AspDotNetMVC/archive/2012/11/16/2767576.html

     1 static byte[] GetBarcode(int height, int width,BarcodeLib.TYPE type,
     2                                           string  code,out System.Drawing.Image image)
     3         {
     4             image=null;
     5             BarcodeLib.Barcode b = new BarcodeLib.Barcode();
     6             b.BackColor = System.Drawing.Color.White;
     7             b.ForeColor = System.Drawing.Color.Black;
     8             b.IncludeLabel = true;
     9             b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
    10             b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
    11             b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
    12             System.Drawing.Font font = new System.Drawing.Font("verdana",10f);
    13             b.LabelFont = font;
    14 
    15             b.Height = height;
    16             b.Width = width;
    17 
    18             image = b.Encode(type, code);
    19             SaveImage(image, Guid.NewGuid().ToString("N")+".png");
    20             byte[] buffer = b.GetImageData(SaveTypes.GIF);
    21             return buffer;
    22         }

    原博主的代码没有贴出保存图片的代码,现在加上

    static  void  BuildBarcode()
           {
               System.Drawing.Image image;
               int width=250, height=100;
               byte[] buffer = GetBarcode(height, width, 
                        BarcodeLib.TYPE.CODE128, "Avx-(13614)-vR", out image);
           }
     1  static byte[] GetBarcode(int height, int width,BarcodeLib.TYPE type,
     2                                           string  code,out System.Drawing.Image image)
     3         {
     4             image=null;
     5             BarcodeLib.Barcode b = new BarcodeLib.Barcode();
     6             b.BackColor = System.Drawing.Color.White;
     7             b.ForeColor = System.Drawing.Color.Black;
     8             b.IncludeLabel = true;
     9             b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
    10             b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
    11             b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
    12             System.Drawing.Font font = new System.Drawing.Font("verdana",10f);
    13             b.LabelFont = font;
    14 
    15             b.Height = height;
    16             b.Width = width;
    17 
    18             image = b.Encode(type, code);
    19             SaveImage(image, Guid.NewGuid().ToString("N")+".png");
    20             byte[] buffer = b.GetImageData(SaveTypes.GIF);
    21             return buffer;
    22         }
    23 
    24      private static void SaveImage(System.Drawing.Image image, string p)
    25         {
    26             //自动保存图片到指定目录中
    27             string stringfilename ="G:\" + p;
    28             image.Save(stringfilename, System.Drawing.Imaging.ImageFormat.Jpeg);
    29         }

    使用该方法还需要一个动态链接库:

    barcode.dll下载 

    生成的条形码:

     

  • 相关阅读:
    JSTL和EL
    SpringMVC 上传文件and过滤器
    SQLServer 大数据查询2
    SQLServer 大数据查询分析
    Oracle分页查询
    Oracle大数据常见优化查询
    Window 下面利用Oid 获取SNMP主机信息 以及计算方法
    window 下面安装net-snmp 简单网关协议
    显示一行省略文字的详细信息
    手机自动隐藏浏览器地址栏
  • 原文地址:https://www.cnblogs.com/robinblogs/p/4485502.html
Copyright © 2020-2023  润新知