protected void Page_Load(object sender, EventArgs e)
{
Bitmap b = GetCode39("BJ-8273-12312-3");
string filename1 = "d:\\BJ-8273-12312-3.jpg";
b.Save(filename1, ImageFormat.Jpeg);
}
private Bitmap GetCode39(string strSource)
{
int x = 5;
int y = 0;
int WidLength = 2;
int NarrowLength = 1;
int BarCodeHeight = 50;
int intSourceLength = strSource.Length;
string strEncode = "010010100";
string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; //Code39的字母
string[] Code39 =
{
/**//* 0 */ "000110100",
/**//* 1 */ "100100001",
/**//* 2 */ "001100001",
/**//* 3 */ "101100000",
/
**//* 4 */ "000110001",
/**//* 5 */ "100110000",
/**//* 6 */ "001110000",
/**//* 7 */ "000100101",
/**//* 8 */ "100100100",
/**//* 9 */ "001100100",
/**//* A */ "100001001",
/**//* B */ "001001001",
/**//* C */ "101001000",
/**//* D */ "000011001",
/**//* E */ "100011000",
/
**//* F */ "001011000",
/**//* G */ "000001101",
/**//* H */ "100001100",
/**//* I */ "001001100",
/**//* J */ "000011100",
/**//* K */ "100000011",
/**//* L */ "001000011",
/**//* M */ "101000010",
/**//* N */ "000010011",
/**//* O */ "100010010",
/**//* P */ "001010010",
/
**//* Q */ "000000111",
/**//* R */ "100000110",
/**//* S */ "001000110",
/**//* T */ "000010110",
/**//* U */ "110000001",
/**//* V */ "011000001",
getsavefilename/**//* W */ "111000000",
/**//* X */ "010010001",
/**//* Y */ "110010000",
/**//* Z */ "011010000",
/**//* - */ "010000101",
/
**//* . */ "110000100",
/**//*' '*/ "011000100",
/**//* $ */ "010101000",
/**//* / */ "010100010",
/**//* + */ "010001010",
/**//* % */ "000101010",
/**//* * */ "010010100"
};
strSource = strSource.ToUpper();
Bitmap objBitmap = new Bitmap(
((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2),
BarCodeHeight + (y * 2));
Graphics objGraphics = Graphics.FromImage(objBitmap);
objGraphics.FillRectangle(Brushes.White, 0, 0, objBitmap.Width, objBi
tmap.Height);
for (int i = 0; i < intSourceLength; i++)
{
if (AlphaBet.IndexOf(strSource[i]) == -1 || strSource[i] == '*')
{
objGraphics.DrawString("含有非法字符",
SystemFonts.DefaultFont, Brushes.Red, x, y);
return objBitmap;
}
strEncode = string.Format("{0}0{1}", strEncode,
Code39[AlphaBet.IndexOf(strSource[i])]);
}
strEncode = string.Format("{0}0010010100", strEncode);
int intEncodeLength = strEncode.Length;
int intBarWidth;
for (int i = 0; i < intEncodeLength; i++)
{
intBarWidth = strEncode[i] == '1' ? WidLength : NarrowLength;
objGraphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White,
x, y, intBarWidth, BarCodeHeight);
x += intBarWidth;
}
return objBitmap;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论