1.工具类:BarCodeHelper(条码生成类),二维码生成类(QRCodeHelper)

2.BarCodeHelper(条码生成类)代码:

   public class BarCodeHelper
{
public class Code128
{
private readonly DataTable _mCode128 = new DataTable();
private uint _mHeight = ; /// <summary>
/// 高度
/// </summary>
public uint Height { get { return _mHeight; } set { _mHeight = value; } }
private Font _mValueFont; /// <summary>
/// 是否显示可见号码 如果为NULL不显示号码
/// </summary>
public Font ValueFont { get { return _mValueFont; } set { _mValueFont = value; } }
private byte _mMagnify; /// <summary>
/// 放大倍数
/// </summary>
public byte Magnify { get { return _mMagnify; } set { _mMagnify = value; } } /// <summary>
/// 条码类别
/// </summary>
public enum Encode
{
Code128A,
Code128B,
Code128C,
Ean128
} public Code128()
{
_mCode128.Columns.Add("ID");
_mCode128.Columns.Add("Code128A");
_mCode128.Columns.Add("Code128B");
_mCode128.Columns.Add("Code128C");
_mCode128.Columns.Add("BandCode");
_mCode128.CaseSensitive = true;
#region 数据表
_mCode128.Rows.Add("", " ", " ", "", "");
_mCode128.Rows.Add("", "!", "!", "", "");
_mCode128.Rows.Add("", "\"", "\"", "", "");
_mCode128.Rows.Add("", "#", "#", "", "");
_mCode128.Rows.Add("", "$", "$", "", "");
_mCode128.Rows.Add("", "%", "%", "", "");
_mCode128.Rows.Add("", "&", "&", "", "");
_mCode128.Rows.Add("", "'", "'", "", "");
_mCode128.Rows.Add("", "(", "(", "", "");
_mCode128.Rows.Add("", ")", ")", "", "");
_mCode128.Rows.Add("", "*", "*", "", "");
_mCode128.Rows.Add("", "+", "+", "", "");
_mCode128.Rows.Add("", ",", ",", "", "");
_mCode128.Rows.Add("", "-", "-", "", "");
_mCode128.Rows.Add("", ".", ".", "", "");
_mCode128.Rows.Add("", "/", "/", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", ":", ":", "", "");
_mCode128.Rows.Add("", ";", ";", "", "");
_mCode128.Rows.Add("", "<", "<", "", "");
_mCode128.Rows.Add("", "=", "=", "", "");
_mCode128.Rows.Add("", ">", ">", "", "");
_mCode128.Rows.Add("", "?", "?", "", "");
_mCode128.Rows.Add("", "@", "@", "", "");
_mCode128.Rows.Add("", "A", "A", "", "");
_mCode128.Rows.Add("", "B", "B", "", "");
_mCode128.Rows.Add("", "C", "C", "", "");
_mCode128.Rows.Add("", "D", "D", "", "");
_mCode128.Rows.Add("", "E", "E", "", "");
_mCode128.Rows.Add("", "F", "F", "", "");
_mCode128.Rows.Add("", "G", "G", "", "");
_mCode128.Rows.Add("", "H", "H", "", "");
_mCode128.Rows.Add("", "I", "I", "", "");
_mCode128.Rows.Add("", "J", "J", "", "");
_mCode128.Rows.Add("", "K", "K", "", "");
_mCode128.Rows.Add("", "L", "L", "", "");
_mCode128.Rows.Add("", "M", "M", "", "");
_mCode128.Rows.Add("", "N", "N", "", "");
_mCode128.Rows.Add("", "O", "O", "", "");
_mCode128.Rows.Add("", "P", "P", "", "");
_mCode128.Rows.Add("", "Q", "Q", "", "");
_mCode128.Rows.Add("", "R", "R", "", "");
_mCode128.Rows.Add("", "S", "S", "", "");
_mCode128.Rows.Add("", "T", "T", "", "");
_mCode128.Rows.Add("", "U", "U", "", "");
_mCode128.Rows.Add("", "V", "V", "", "");
_mCode128.Rows.Add("", "W", "W", "", "");
_mCode128.Rows.Add("", "X", "X", "", "");
_mCode128.Rows.Add("", "Y", "Y", "", "");
_mCode128.Rows.Add("", "Z", "Z", "", "");
_mCode128.Rows.Add("", "[", "[", "", "");
_mCode128.Rows.Add("", "\\", "\\", "", "");
_mCode128.Rows.Add("", "]", "]", "", "");
_mCode128.Rows.Add("", "^", "^", "", "");
_mCode128.Rows.Add("", "_", "_", "", "");
_mCode128.Rows.Add("", "NUL", "`", "", "");
_mCode128.Rows.Add("", "SOH", "a", "", "");
_mCode128.Rows.Add("", "STX", "b", "", "");
_mCode128.Rows.Add("", "ETX", "c", "", "");
_mCode128.Rows.Add("", "EOT", "d", "", "");
_mCode128.Rows.Add("", "ENQ", "e", "", "");
_mCode128.Rows.Add("", "ACK", "f", "", "");
_mCode128.Rows.Add("", "BEL", "g", "", "");
_mCode128.Rows.Add("", "BS", "h", "", "");
_mCode128.Rows.Add("", "HT", "i", "", "");
_mCode128.Rows.Add("", "LF", "j", "", "");
_mCode128.Rows.Add("", "VT", "k", "", "");
_mCode128.Rows.Add("", "FF", "I", "", "");
_mCode128.Rows.Add("", "CR", "m", "", "");
_mCode128.Rows.Add("", "SO", "n", "", "");
_mCode128.Rows.Add("", "SI", "o", "", "");
_mCode128.Rows.Add("", "DLE", "p", "", "");
_mCode128.Rows.Add("", "DC1", "q", "", "");
_mCode128.Rows.Add("", "DC2", "r", "", "");
_mCode128.Rows.Add("", "DC3", "s", "", "");
_mCode128.Rows.Add("", "DC4", "t", "", "");
_mCode128.Rows.Add("", "NAK", "u", "", "");
_mCode128.Rows.Add("", "SYN", "v", "", "");
_mCode128.Rows.Add("", "ETB", "w", "", "");
_mCode128.Rows.Add("", "CAN", "x", "", "");
_mCode128.Rows.Add("", "EM", "y", "", "");
_mCode128.Rows.Add("", "SUB", "z", "", "");
_mCode128.Rows.Add("", "ESC", "{", "", "");
_mCode128.Rows.Add("", "FS", "|", "", "");
_mCode128.Rows.Add("", "GS", "}", "", "");
_mCode128.Rows.Add("", "RS", "~", "", "");
_mCode128.Rows.Add("", "US", "DEL", "", "");
_mCode128.Rows.Add("", "FNC3", "FNC3", "", "");
_mCode128.Rows.Add("", "FNC2", "FNC2", "", "");
_mCode128.Rows.Add("", "SHIFT", "SHIFT", "", "");
_mCode128.Rows.Add("", "CODEC", "CODEC", "", "");
_mCode128.Rows.Add("", "CODEB", "FNC4", "CODEB", "");
_mCode128.Rows.Add("", "FNC4", "CODEA", "CODEA", "");
_mCode128.Rows.Add("", "FNC1", "FNC1", "FNC1", "");
_mCode128.Rows.Add("", "StartA", "StartA", "StartA", "");
_mCode128.Rows.Add("", "StartB", "StartB", "StartB", "");
_mCode128.Rows.Add("", "StartC", "StartC", "StartC", "");
_mCode128.Rows.Add("", "Stop", "Stop", "Stop", "");
#endregion
} /// <summary>
/// 获取128图形
/// </summary>
/// <param name="pText">文字</param>
/// <param name="pCode">编码</param>
/// <returns>图形</returns>
public Bitmap GetCodeImage(string pText, Encode pCode)
{
string viewText = pText;
string text = "";
IList<int> textNumb = new List<int>();
int examine; //首位
switch (pCode)
{
case Encode.Code128C:
examine = ;
if ((pText.Length & ) != ) throw new Exception("128C长度必须是偶数");
while (pText.Length != )
{
var temp = ;
try
{
var codeNumb128 = Int32.Parse(pText.Substring(, ));
}
catch
{
throw new Exception("128C必须是数字!");
}
text += GetValue(pCode, pText.Substring(, ), ref temp);
textNumb.Add(temp);
pText = pText.Remove(, );
}
break;
case Encode.Ean128:
examine = ;
if ((pText.Length & ) != ) throw new Exception("EAN128长度必须是偶数");
textNumb.Add();
text += "";
while (pText.Length != )
{
var temp = ;
try
{
var codeNumb128 = Int32.Parse(pText.Substring(, ));
}
catch
{
throw new Exception("128C必须是数字!");
}
text += GetValue(Encode.Code128C, pText.Substring(, ), ref temp);
textNumb.Add(temp);
pText = pText.Remove(, );
}
break;
default:
examine = pCode == Encode.Code128A ? : ; while (pText.Length != )
{
var temp = ;
var valueCode = GetValue(pCode, pText.Substring(, ), ref temp);
if (valueCode.Length == ) throw new Exception("无效的字符集!" + pText.Substring(, ));
text += valueCode;
textNumb.Add(temp);
pText = pText.Remove(, );
}
break;
}
if (textNumb.Count == ) throw new Exception("错误的编码,无数据");
text = text.Insert(, GetValue(examine)); //获取开始位 for (int i = ; i != textNumb.Count; i++)
{
examine += textNumb[i] * (i + );
}
examine = examine % ; //获得严效位
text += GetValue(examine); //获取严效位
text += ""; //结束位
var codeImage = GetImage(text);
GetViewText(codeImage, viewText);
return codeImage;
} /// <summary>
/// 获取目标对应的数据
/// </summary>
/// <param name="pCode">编码</param>
/// <param name="pValue">数值 A b 30</param>
/// <param name="pSetId">返回编号</param>
/// <returns>编码</returns>
private string GetValue(Encode pCode, string pValue, ref int pSetId)
{
if (_mCode128 == null) return "";
var row = _mCode128.Select(pCode + "='" + pValue + "'");
if (row.Length != ) throw new Exception("错误的编码" + pValue);
pSetId = Int32.Parse(row[]["ID"].ToString());
return row[]["BandCode"].ToString();
} /// <summary>
/// 根据编号获得条纹
/// </summary>
/// <param name="pCodeId"></param>
/// <returns></returns>
private string GetValue(int pCodeId)
{
var row = _mCode128.Select("ID='" + pCodeId + "'");
if (row.Length != ) throw new Exception("验效位的编码错误" + pCodeId);
return row[]["BandCode"].ToString();
} /// <summary>
/// 获得条码图形
/// </summary>
/// <param name="pText">文字</param>
/// <returns>图形</returns>
private Bitmap GetImage(string pText)
{
var value = pText.ToCharArray();
var width = ;
for (int i = ; i != value.Length; i++)
{
width += Int32.Parse(value[i].ToString(CultureInfo.InvariantCulture)) * (_mMagnify + );
}
var codeImage = new Bitmap(width, (int)_mHeight);
var garphics = Graphics.FromImage(codeImage);
//Pen _Pen;
var lenEx = ;
for (int i = ; i != value.Length; i++)
{
int valueNumb = Int32.Parse(value[i].ToString(CultureInfo.InvariantCulture)) * (_mMagnify + ); //获取宽和放大系数
garphics.FillRectangle((i & ) != ? Brushes.White : Brushes.Black,
new Rectangle(lenEx, , valueNumb, (int)_mHeight));
//_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
lenEx += valueNumb;
}
garphics.Dispose();
return codeImage;
} /// <summary>
/// 显示可见条码文字 如果小于40 不显示文字
/// </summary>
/// <param name="pBitmap">图形</param>
/// <param name="pViewText"></param>
private void GetViewText(Bitmap pBitmap, string pViewText)
{
if (_mValueFont == null) return; Graphics graphics = Graphics.FromImage(pBitmap);
SizeF drawSize = graphics.MeasureString(pViewText, _mValueFont);
if (drawSize.Height > pBitmap.Height - || drawSize.Width > pBitmap.Width)
{
graphics.Dispose();
return;
} int starY = pBitmap.Height - (int)drawSize.Height;
graphics.FillRectangle(Brushes.White, new Rectangle(, starY, pBitmap.Width, (int)drawSize.Height));
graphics.DrawString(pViewText, _mValueFont, Brushes.Black, , starY);
}
}
}

3.二维码生成类(QRCodeHelper)引用:ThoughtWorks.QRCode.dll

  public class QRCodeHelper
{
private System.Drawing.Printing.PrintPageEventArgs e;
private int Top = ;//提供一个基准Top,用户不提供Top时,就取该Top,每次有内容增加都会刷新
private int Left = ;//提供一个基准Left,用户不提供Left时,就取该Left
private int Interval = ;//提供一个基准间隔值 #region 根据链接生成二维码
/// <summary>
/// 根据链接获取二维码
/// 所需参数:URL;距左Left值;距上Top值
/// 默认值:无
/// </summary>
/// <param name="url">链接</param
/// <param name="left">二维码:x</param>
/// <param name="top">二维码:y</param>
/// <returns>返回二维码图片的高度</returns>
public int DrawQRCodeBmp(string url, int left, int top)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = ;
qrCodeEncoder.QRCodeVersion = ;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
Image bmp = qrCodeEncoder.Encode(url);
e.Graphics.DrawImage(bmp, new Point(left, top));//不同的URL图片大小不同,可以根据需要调整left坐标 Top += bmp.Height + this.Interval; return bmp.Height;
} /// <summary>
/// 根据链接获取二维码
/// 所需参数:URL;距左Left值
/// 默认值:距上Top值:顺位值
/// </summary>
/// <param name="url">链接</param
/// <param name="left">二维码:x</param>
/// <returns>返回二维码图片的高度</returns>
public int DrawQRCodeBmp(string url, int left)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = ;
qrCodeEncoder.QRCodeVersion = ;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
Image bmp = qrCodeEncoder.Encode(url);
e.Graphics.DrawImage(bmp, new Point(left, this.Top));//不同的URL图片大小不同,可以根据需要调整left坐标 Top += bmp.Height + this.Interval; return bmp.Height;
}
/// <summary>
/// 根据链接获取二维码
/// 所需参数:URL
/// 默认值:距左Left值;距上Top值:顺位值
/// </summary>
/// <param name="url">链接</param>
/// <returns></returns>
//public int DrawQRCodeBmp(string url)
//{
// QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
// qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
// qrCodeEncoder.QRCodeScale = 4;
// qrCodeEncoder.QRCodeVersion = 0;
// qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
// Image bmp = qrCodeEncoder.Encode(url);
// e.Graphics.DrawImage(bmp, new Point(Left, Top));//不同的URL图片大小不同,可以根据需要调整left坐标 // Top += bmp.Height + this.Interval; // return bmp.Height;
//}
#endregion public Bitmap DrawQRCodeBmp(string url)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = ;
qrCodeEncoder.QRCodeVersion = ;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
return qrCodeEncoder.Encode(url);
}
}

4.form窗体调用:

  /// <summary>
/// 获取条形码保存路径
/// </summary>
/// <returns></returns>
private string GetBarCodeMap()
{
var path = string.Empty;
var code = DateTime.Now.ToString("yyyymmddhhmmss"); path = System.AppDomain.CurrentDomain.BaseDirectory + "BarCode\\" + Guid.NewGuid().ToString() + ".gif"; Code128 _Code = new Code128();
_Code.Height = ;
_Code.ValueFont = new Font("宋体", );
System.Drawing.Bitmap imgTemp = _Code.GetCodeImage(code, Code128.Encode.Code128A);
imgTemp.Save(path, System.Drawing.Imaging.ImageFormat.Gif); return path;
} /// <summary>
/// 获取二维码保存路径
/// </summary>
/// <returns></returns>
private string GetQRCodeMap()
{
var path = string.Empty;
var url = "www.baidu.com"; path = System.AppDomain.CurrentDomain.BaseDirectory + "BarCode\\" + Guid.NewGuid().ToString() + ".gif"; QRCodeHelper _QRCode = new QRCodeHelper();
System.Drawing.Bitmap imgTemp = _QRCode.DrawQRCodeBmp(url);
imgTemp.Save(path, System.Drawing.Imaging.ImageFormat.Gif); return path;
}
05-11 17:49