如何采用C#绘制39条形码
2008-08-24 23:46
357 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace CjLibrary
{
public enum AlignType
{
Left = 0,
Right = 1,
Center = 2
}
public class CjBarCode
{
private AlignType align;
public AlignType Align
{
get { return align; }
set { align = value; }
}
private int leftMargin = 10;
public int LeftMargin
{
get { return leftMargin; }
set { leftMargin = value; }
}
private int topMargin = 10;
public int TopMargin
{
get { return topMargin; }
set { topMargin = value; }
}
private int barcodeHeight = 50;
public int BarcodeHeight
{
get { return barcodeHeight; }
set { barcodeHeight = value; }
}
private string headerText="浙江绍兴";
public string HeaderText
{
get { return headerText; }
set { headerText = value; }
}
private string barcodeText = "";
public string BarcodeText
{
get { return barcodeText; }
set { barcodeText = value; }
}
private bool showHeader;
public bool ShowHeader
{
get { return showHeader; }
set { showHeader = value; }
}
private bool showFooter;
public bool ShowFooter
{
get { return showFooter; }
set { showFooter = value; }
}
private int headerFontSize;
public int HeaderFontSize
{
get { return headerFontSize; }
set { headerFontSize = value; }
}
private int footerFontSize;
public int FooterFontSize
{
get { return footerFontSize; }
set { footerFontSize = value; }
}
private string headerFontFamily;
public string HeaderFontFamily
{
get { return headerFontFamily; }
set { headerFontFamily = value; }
}
private string footerFontFamily;
public string FooterFontFamily
{
get { return footerFontFamily; }
set { footerFontFamily = value; }
}
protected Font headerFont=null;
protected Font footerFont=null;
public CjBarCode()
{
align = AlignType.Center;
headerFontFamily = "Courier";
footerFontFamily = "Courier";
headerFontSize = 9;
footerFontSize = 9;
headerFont = new Font(headerFontFamily, headerFontSize);
footerFont = new Font(footerFontFamily, footerFontSize);
}
protected virtual void DrawBitmap(Graphics g, Rectangle rects)
{
SolidBrush brush = new SolidBrush(Color.PowderBlue);
g.FillRectangle(brush, rects);
}
protected virtual void DrawText(string text,Graphics g, Rectangle rects)
{
SolidBrush brush = new SolidBrush(Color.PowderBlue);
g.FillRectangle(brush, rects);
}
}
public class CjBarCode39 : CjBarCode
{
private double wideToNarrowRatio = 3.0;
public double WideToNarrowRatio
{
get { return wideToNarrowRatio; }
set { wideToNarrowRatio = value; }
}
private int weight = 1;
public int Weight
{
get { return weight; }
set { weight = value; }
}
/// <summary>
/// 39条码中能使用的字符
/// </summary>
private String alphabet39 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*";
private String[] coded39Char =
{
/* 0 */ "001100100",
/* 1 */ "100010100",
/* 2 */ "010010100",
/* 3 */ "110000100",
/* 4 */ "001010100",
/* 5 */ "101000100",
/* 6 */ "011000100",
/* 7 */ "000110100",
/* 8 */ "100100100",
/* 9 */ "010100100",
/* A */ "100010010",
/* B */ "010010010",
/* C */ "110000010",
/* D */ "001010010",
/* E */ "101000010",
/* F */ "011000010",
/* G */ "000110010",
/* H */ "100100010",
/* I */ "010100010",
/* J */ "001100010",
/* K */ "100010001",
/* L */ "010010001",
/* M */ "110000001",
/* N */ "001010001",
/* O */ "101000001",
/* P */ "011000001",
/* Q */ "000110001",
/* R */ "100100001",
/* S */ "010100001",
/* T */ "001100001",
/* U */ "100011000",
/* V */ "010011000",
/* W */ "110001000",
/* X */ "001011000",
/* Y */ "101001000",
/* Z */ "011001000",
/* - */ "000111000",
/* . */ "110000100",
/*' '*/ "011000100",
/* $ */ "010101000",
/* / */ "010100010",
/* + */ "010001010",
/* % */ "100101000",
/* * */ "001101000"
};
public CjBarCode39()
{
BarcodeText = "1234";
}
/// <summary>
/// 为了使得条形码居中先要计算条形码的Left和Top坐标
/// </summary>
/// <returns></returns>
private int getX()
{
int currentLocation = 0;
string strBarcode = "*" + BarcodeText.ToUpper() + "*";
for (int i = 0; i < strBarcode.Length; i++)
{
string encodedString = coded39Char[alphabet39.IndexOf(strBarcode[i])];
for (int j = 0; j < 5; j++)
{
if (encodedString[j] == '0')
{
currentLocation += weight;
}
else
{
currentLocation += 3 * weight;
}
//画第6个 5 白条
if ((j + 5) < 9)
{
if (encodedString[j + 5] == '0')
{
currentLocation += weight;
}
else
{
currentLocation += 3 * weight;
}
}
}
currentLocation += weight;
}
return currentLocation;
}
/// <summary>
/// 显示条形码
/// </summary>
/// <param name="g">GDI+</param>
/// <param name="rects">画图区域</param>
protected override void DrawBitmap(Graphics g, Rectangle rects)
{
if (BarcodeText == "") return;
string strBarcode = "*" + BarcodeText.ToUpper() + "*";
//string strBarcode = BarcodeText.ToUpper() ;
String encodedString = "";
int currentLocation =(rects.Width- getX())/2;
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush witeBrush = new SolidBrush(Color.White);
int yTop = rects.Y;
for (int i = 0; i < strBarcode.Length; i++)
{
encodedString = coded39Char[alphabet39.IndexOf(strBarcode[i])];
for (int j = 0; j < 5; j++)
{
if (encodedString[j] == '0')
{
Rectangle re1 = new Rectangle(currentLocation, yTop, weight, BarcodeHeight);
g.FillRectangle(blackBrush, re1);
currentLocation += weight;
}
else
{
Rectangle re1 = new Rectangle(currentLocation, yTop, 3 * weight, BarcodeHeight);
g.FillRectangle(blackBrush, re1);
currentLocation += 3 * weight;
}
//画第6个 5 白条
if ((j + 5) < 9)
{
if (encodedString[j + 5] == '0')
{
Rectangle re1 = new Rectangle(currentLocation, yTop, weight, BarcodeHeight);
g.FillRectangle(witeBrush, re1);
currentLocation += weight;
}
else
{
Rectangle re1 = new Rectangle(currentLocation, yTop, 3 * weight, BarcodeHeight);
g.FillRectangle(witeBrush, re1);
currentLocation += 3 * weight;
}
}
}
Rectangle re2 = new Rectangle(currentLocation, yTop, weight, BarcodeHeight);
g.FillRectangle(witeBrush, re2);
currentLocation += weight;
}
}
/// <summary>
/// 显示条形码和文字
/// </summary>
/// <param name="g"></param>
/// <param name="rects"></param>
public void DrawBarcode(Graphics g, Rectangle rects)
{
SizeF hsize = g.MeasureString(HeaderText, this.headerFont);
SizeF fsize = g.MeasureString(BarcodeText, this.footerFont);
//HeadText
Rectangle t = new Rectangle();
t = rects;
t.Height = (int)hsize.Height;
t.X = (rects.Width - (int)hsize.Width) / 2;
DrawText(HeaderText, g, t);
//FootText
Rectangle b = new Rectangle();
b = rects;
b.Y = rects.Height - (int)fsize.Height;
b.X = (rects.Width - (int)fsize.Width) / 2;
b.Height = (int)fsize.Height;
DrawText("*" + BarcodeText + "*", g, b);
//BarCode
Rectangle m = new Rectangle();
m = rects;
m.Y = t.Height;
m.Height = rects.Height - t.Height - b.Height;
this.BarcodeHeight = m.Height;
DrawBitmap(g, m);
}
/// <summary>
/// 文本显示
/// </summary>
/// <param name="text"></param>
/// <param name="g"></param>
/// <param name="rects"></param>
protected override void DrawText(string text, Graphics g, Rectangle rects)
{
g.DrawString(text, this.headerFont, Brushes.Black, rects);
}
}
}
相关文章推荐
- SkylineGlobe 6.5 如何实现简单多边形的动态绘制 C#示例代码
- C#如何打印条形码EAN13码
- C#中如何获取其他进程的命令行参数 ( How to get other processes's command line argument )
- C#开发教程--如何绘制箭头
- C#绘制条形码的代码
- C# 如何在PDF文档中添加一维条形码和二维条形码
- C# 条形码(Barcode)的绘制(附件下载)
- 【转载】C#下如何实现 绘制曲线图和柱状图
- 如何绘制贝塞尔样条曲线和基数样条曲线[C#]
- C# 画(绘制)直线 C#如何画直线 C#绘制直线 WPF 画(绘制)直线
- C#如何打印条形码EAN13码
- 如何通过ODBC驱动代码连接Sql Server数据库--采用C#语言
- C# 条形码(Barcode)的绘制(附件下载)
- C# 制作39条形码
- C#.NET常见问题(FAQ)-如何使用2D绘图控件ZedGraph绘制坐标轴和坐标曲线
- 如何在水晶报表中添加条形码-winform ,c#.net
- C#中如何实现AVI视频的播放并且需要在播放视频的同时在视频上进行一些简单线条的绘制?
- 【C#】第3章补充(一)如何在WPF中绘制正弦曲线
- C# richTextBox如何控制行高的问题
- C#中如何使用FindWindow来查找Word,计算器,Notepad等软件