c# 条形码 [求指教]

因公司需要完成一条形码打印问题,所以在找到一些资料做了一个Demo

特请教!

不知道此条形码是否正确:

图:

 

 

源码:

 

ExpandedBlockStart.gif 代码
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Drawing;

namespace  Paabo.WordProcessing.Common
{
    
public   class  BarCodeProvider
    {
        
#region  单例
        
private   static  BarCodeProvider _Intance;
        
public   static  BarCodeProvider Intance
        {
            
get
            {
                
if  (_Intance  ==   null )
                {
                    _Intance 
=   new  BarCodeProvider();
                }
                
return  _Intance;
            }
        }
        
#endregion

        
#region  Size
        
///   <summary>
        
///  图片宽度
        
///   </summary>
         private   int  _Width  =   200 ;
        
public   int  Width
        {
            
get  {  return  _Width; }
            
set  { _Width  =  value; }
        }


        
///   <summary>
        
///  图片高度
        
///   </summary>
         private   int  _Height  =   80 ;
        
public   int  Height 
        {
            
get  {  return  _Height; }
            
set  { _Height  =  value; }
        }

        
///   <summary>
        
///  明文高度
        
///   </summary>
         private   int  _TextHeight  =   25 ;
        
public   int  TextHeight
        {
            
get  {  return  _TextHeight; }
            
set  { _TextHeight  =  value; }
        }

        
#endregion

        
#region  边距
        
private   int  _Margin_Top  =   5 ;

        
///   <summary>
        
///  上边距
        
///   </summary>
         public   int  Margin_Top
        {
            
get  {  return  _Margin_Top; }
            
set  { _Margin_Top  =  value; }
        }

        
private   int  _Margin_Left  =   5 ;

        
///   <summary>
        
///  左边距
        
///   </summary>
         public   int  Margin_Left
        {
            
get  {  return  _Margin_Left; }
            
set  { _Margin_Left  =  value; }
        }


        
private   int  _Margin_Right  =   5 ;

        
///   <summary>
        
///  右边距
        
///   </summary>
         public   int  Margin_Right
        {
            
get  {  return  _Margin_Right; }
            
set  { _Margin_Right  =  value; }
        }

        
private   int  _Margin_Bottom  =   5 ;
        
///   <summary>
        
///  下边距
        
///   </summary>
         public   int  Margin_Bottom
        {
            
get  {  return  _Margin_Bottom; }
            
set  { _Margin_Bottom  =  value; }
        }
        
#endregion

        
private  Font _TextFont  =   new  Font( " 宋体 " 12 );

        
///   <summary>
        
///  明文字体
        
///   </summary>
         public  Font TextFont
        {
            
get  {  return  _TextFont; }
            
set  { _TextFont  =  value; }
        }

        
private  Pen _BlackPen  =   new  Pen(Brushes.Black);
        
private  Pen _WhitePen  =   new  Pen(Brushes.White);

        
///   <summary>
        
///  将明文装换为编码
        
///   </summary>
        
///   <param name="text"> 明文内容 </param>
        
///   <returns> 编码 </returns>
         private   string  ConvertToBarCode( string  text)
        {
            
string  code  =   string .Empty;
            
foreach  ( char  item  in  text)
            {
                
int  itemValue  =  item;
                code 
+=  Convert.ToString(itemValue,  2 +   " , " ;
            }
            
return  code;
        }

        
///   <summary>
        
///  将字符串生成条形图片
        
///   </summary>
        
///   <param name="text"> 明文内容 </param>
        
///   <returns></returns>
         public  Bitmap CreateBarCodeImage( string  text)
        {
            Bitmap map 
=   new  Bitmap(Width, Height);
            Graphics g 
=  Graphics.FromImage(map);
            
try
            {
                
string  code  =  ConvertToBarCode(text);
                code 
=   string .Format( " 101{0}101 " , code);
                
char [] array  =  code.ToCharArray();
                
char [] textArray  =  text.ToCharArray();
                
int  lineWidth  =  (Width  -  Margin_Left - Margin_Right)  /  (array.Length  -  textArray.Length);
                
int  lineHeight  =  Height  -  TextHeight  -  Margin_Bottom;

                _BlackPen.Width 
=  lineWidth;
                _WhitePen.Width 
=  lineWidth;

                
int  x  =   5 ;
                
int  topY  =   5 ;
                
int  bottonY  =  Height  -  Margin_Bottom  -  TextHeight;

                
int  index  =   0 ;
                
char  pItem  =   '   ' ;
                Pen pen 
=   null ;
                
foreach  ( char  item  in  array)
                {

                    
if  (item  ==   ' , ' )
                    {
                        
string  t  =  textArray[index].ToString();
                        g.DrawString(t, TextFont, Brushes.Black, 
new  PointF(x  -  (lineWidth  *   5 ), bottonY  +   3 ));
                        index
++ ;
                    }
                    
else
                    {
                        
if  (pItem  ==   '   ' )
                        {
                            pen 
=  _BlackPen;
                        }
                        
else
                        {
                            
if  (item  !=  pItem)
                            {
                                pen 
=  pen  ==  _BlackPen  ?  _WhitePen : _BlackPen;
                            }
                        }
                        pItem 
=  item;
                        g.DrawLine(pen, 
new  Point(x, topY),  new  Point(x, bottonY));
                    }
                    x 
+=  lineWidth;
                }
            }
            
catch  (Exception ex)
            {
                g.Clear(Color.White);
                g.DrawString(ex.Message, TextFont, Brushes.Black, 
new  PointF( 0 0 ));
            }
            g.Save();
            
return  map;
        }
    }
}

 

本人联系方式为:paabo@live.cn  恭候各位指教。

转载于:https://www.cnblogs.com/duoluodaizi/archive/2010/06/24/1764176.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值