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下载 

生成的条形码:

 

posted @ 2015-05-07 17:21  夜了盖咋啦  阅读(3942)  评论(2编辑  收藏  举报