C#在WINForm中创建39码和交错式25码条形码图片,打印条形码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Drawing;

using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;

namespace TXM
{
public class BarCode
{
PrintDialog printDialog = new PrintDialog();
PrintDocument printDocument = new PrintDocument();
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();


public BarCode()
{
printDocument.PrintPage+=new PrintPageEventHandler(printDocument_PrintPage);
}


///


/// 生成条形码的字符串
///

/// 检验单号
/// LIS链接的系统类型
/// 就诊类型
/// 生成条形码的字符串
public string CreateBarCode(string s1, string s2, string s3)
{
return s1 + s2 + s3;
}

#region 39码
///
/// 得到39码的图像
///

/// 输入的字符串
/// 图片所在的路径
public string get39(string s)
{
Pen PenBlack = new Pen(Brushes.Black);//黑色线
Pen PenWhite =new Pen(Brushes.White);//白色线
PenBlack.Width=1;//统一的黑色线的宽
PenWhite.Width=1;//统一的白色线的宽

Bitmap bm = new Bitmap(200, 70);//定义画布的像素(长,高)

Graphics g = Graphics.FromImage(bm);//声明绘图
g.Clear(Color.White);//定义绘图的背景色为白色
#region 自定义字符的二进制格式
Hashtable ht = new Hashtable();//自定义字符的二进制的格式
#region 39码 12位
ht.Add('A', "110101001011");
ht.Add('B', "101101001011");
ht.Add('C', "110110100101");
ht.Add('D', "101011001011");
ht.Add('E', "110101100101");
ht.Add('F', "101101100101");
ht.Add('G', "101010011011");
ht.Add('H', "110101001101");
ht.Add('I', "101101001101");
ht.Add('J', "101011001101");
ht.Add('K', "110101010011");
ht.Add('L', "101101010011");
ht.Add('M', "110110101001");
ht.Add('N', "101011010011");
ht.Add('O', "110101101001");
ht.Add('P', "101101101001");
ht.Add('Q', "101010110011");
ht.Add('R', "110101011001");
ht.Add('S', "101101011001");
ht.Add('T', "101011011001");
ht.Add('U', "110010101011");
ht.Add('V', "100110101011");
ht.Add('W', "110011010101");
ht.Add('X', "100101101011");
ht.Add(

'Y', "110010110101");
ht.Add('Z', "100110110101");
ht.Add('0', "101001101101");
ht.Add('1', "110100101011");
ht.Add('2', "101100101011");
ht.Add('3', "110110010101");
ht.Add('4', "101001101011");
ht.Add('5', "110100110101");
ht.Add('6', "101100110101");
ht.Add('7', "101001011011");
ht.Add('8', "110100101101");
ht.Add('9', "101100101101");
ht.Add('+', "100101001001");
ht.Add('-', "100101011011");
ht.Add('*', "100101101101");
ht.Add('/', "100100101001");
ht.Add('%', "101001001001");
ht.Add('$', "100100100101");
ht.Add('.', "110010101101");
ht.Add(' ', "100110101101");
#endregion
#region 39码 9位
////ht.Add('0', "000110100");
////ht.Add('1', "100100001");
////ht.Add('2', "001100001");
////ht.Add('3', "101100000");
////ht.Add('4', "000110001");
////ht.Add('5', "100110000");
////ht.Add('6', "001110000");
////ht.Add('7', "000100101");
////ht.Add('8', "100100100");
////ht.Add('9', "001100100");
////ht.Add('A', "100001001");
////ht.Add('B', "001001001");
////ht.Add('C', "101001000");
////ht.Add('D', "000011001");
////ht.Add('E', "100011000");
////ht.Add('F', "001011000");
////ht.Add('G', "000001101");
////ht.Add('H', "100001100");
////ht.Add('I', "001001100");
////ht.Add('J', "000011100");
////ht.Add('K', "100000011");
////ht.Add('L', "001000011");
////ht.Add('M', "101000010");
////ht.Add('N', "000010011");
////ht.Add('O', "100010010");
////ht.Add('P', "001010010");
////ht.Add('Q', "000000111");
////ht.Add('R', "100000110");
////ht.Add('S', "001000110");
////ht.Add('T', "000010110");
////ht.Add('U', "110000001");
////ht.Add('V', "011000001");
////ht.Add('W', "111000000");
////ht.Add('X', "010010001");
////ht.Add('Y', "110010000");
////ht.Add('Z', "011010000");
////ht.Add('-', "010000101");
////ht.Add('.', "110000100");
////ht.Add(' ', "011000100");
////ht.Add('*', "010010100");
////ht.Add('$', "010101000");
////ht.Add('/', "010100010");
////ht.Add('+', "010001010");
////ht.Add('%', "000101010");
#endregion
#endregion
float w = 1F;//条形码单个一条的宽度
float h = 40F;//条形码单个一条的高度
s = "*" + s.ToString().ToUpper() + "*";//39码格式

的定义为“*……*”;
//s = s.ToString().ToUpper();
string result_bin = "";//把字符的二进制toString()显示,"00"表示开始标签
try
{
foreach (char ch in s)
{
result_bin += ht[ch].ToString();
result_bin += "0";//规定每个字符隔开一条白线
result_bin += ",";//为以后的DrawString(即:画字符串)做准备(因为每个字符要求隔开)
}
//result_bin += "10";//"10"表示结束标签
}
catch
{

g.DrawString("存在不允许的字符", new Font("宋体", 5), Brushes.Black, new PointF(0, 50));//当出现错误时,通过画布显示

g.Dispose();
bm.Save("11.jpg");
string path2 = Application.StartupPath + "\\11.jpg";
return path2;
}
Pen thePen = null;
float x = w;//画布逐渐向右扩展的宽为线条的宽
float x2 = 0;//字符的X坐标的逐渐向右扩展的初始值
float topY = 1F;//线条顶点的Y坐标
float chInt = 0;//为DrawString 做准备
float len = ht['1'].ToString().Length;//任意单个字符的二进制的长度
foreach (char c in result_bin)
{
if (c != ',')
{
thePen = c == '0' ? PenWhite : PenBlack;//当二进制为“0”时,显示白线,当二进制为“1”时,显示黑线
g.DrawLine(thePen, new PointF(x, topY), new PointF(x, 1F + h));//根据上边的线条,画在画布上
x += w;
x2 += w;
}
//打印字符串
else if (c == ',')
{

int chint2 = 0;
foreach (char thec in s)
{
if (chint2 == chInt)
{
g.DrawString(thec.ToString(), new Font("宋体", 5), Brushes.Black, new PointF(x2 - ((len + 1) * 1), 2 + h + 1));//在画布上显示文字
}
chint2 = chint2 + 1;
}
chInt = chInt + 1;
}
}
g.Dispose();
bm.Save("11.jpg");
string path = Application.StartupPath + "\\11.jpg";
return path;
//return path;

}
#endregion
///


/// 得到交错式25码的图像
///

/// 输入的字符串
/// 图片所在的路径
public string get25(string s)
{
Pen PenBlack = new Pen(Brushes.Blac

k);//黑色线
Pen PenWhite = new Pen(Brushes.White);//白色线
PenBlack.Width = 1;//统一的黑色线的宽
PenWhite.Width = 1;//统一的白色线的宽

Bitmap bm = new Bitmap(200, 100);//定义画布的像素(长,高)

Graphics g = Graphics.FromImage(bm);//声明绘图
g.Clear(Color.White);//定义绘图的背景色为白色
#region 自定义字符的二进制格式
Hashtable ht = new Hashtable();//自定义字符的二进制的格式
#region 交错式25码
ht.Add('1', "10001");
ht.Add('2', "01001");
ht.Add('3', "11000");
ht.Add('4', "00101");
ht.Add('5', "10100");
ht.Add('6', "01100");
ht.Add('7', "00011");
ht.Add('8', "10010");
ht.Add('9', "01010");
ht.Add('0', "00110");
//开始符为"00"
//结束符为"10"
#endregion

#endregion
float w = 1F;//条形码单个一条的宽度
float h = 40F;//条形码单个一条的高度
s = s.ToString().ToUpper();
string s1 = "";//上较差的字符串
string s2 = "";//下较差的字符串
for (int i = 0; i < s.Length; i++)
{
if (i % 2 == 0)
{
s1 += s.Substring(i, 1);
}
else if (i % 2 == 1)
{
s2 += s.Substring(i, 1);
}
}

#region 检查字符串
string result_bin = "00,";//把字符的二进制toString()显示,"00"表示开始标签
try
{
foreach (char ch in s)
{
result_bin += ht[ch].ToString();
result_bin += ",";//为以后的DrawString(即:画字符串)做准备(因为每个字符要求隔开)
}
result_bin += "10";//"10"表示结束标签
}
catch
{

g.DrawString("存在不允许的字符", new Font("宋体", 5), Brushes.Black, new PointF(0, 50));//当出现错误时,通过画布显示

g.Dispose();
bm.Save("11.jpg");
string path2 = Application.StartupPath + "\\11.jpg";
return path2;
}

#endregion
#region 上较差字符串的二进制格式
string result_s1 = "";
string result_s2 = "";
foreach (char ch in s1)
{
result_s1 += ht[ch].ToString();

}
#endregion
#region 下较差字符串的二进制格式
foreach (char ch in s2)
{
result_s2

+= ht[ch].ToString();

}
#endregion
string zongS = "";//result_s1和result_s2之和
int j = 0;//为计算基数时,画图位置
for (int i = 0; i < result_s1.Length + result_s2.Length; i++)
{
if (i % 2 == 0)
{
zongS += result_s1.Substring((i/2), 1);
}
else
{
j = j + 1;
zongS += result_s2.Substring(i - j, 1);
}
}
Pen Pen1 = PenBlack;//上较差的颜色为黑色
Pen Pen2 = PenWhite;//下较差的颜色为白色
float x = 0F;
float topY = 1F;
#region 画开始符
for (int i = 0; i < 4; i++)
{
if (i % 2 == 0)
{

g.DrawLine(Pen1, new PointF(x, topY), new PointF(x, topY + h));//根据上边的线条,画在画布上
x += w;
}
else
{
g.DrawLine(Pen2, new PointF(x, topY), new PointF(x, topY + h));//根据上边的线条,画在画布上
x += w;
}
}
#endregion
#region 画条形码得到的数字
int z=0;//记录画线的次数
foreach (char c in zongS)
{
if (z % 2 == 0)
{

if (c == '0')
{

g.DrawLine(Pen1, new PointF(x, topY), new PointF(x, topY + h));
x += w;
}
else if (c == '1')
{
g.DrawLine(Pen1, new PointF(x, topY), new PointF(x, topY + h));
x += w;
g.DrawLine(Pen1, new PointF(x, topY), new PointF(x, topY + h));
x += w;

}

}
else
{
if (c == '0')
{
g.DrawLine(Pen2, new PointF(x, topY), new PointF(x, topY + h));
x += w;
}
else if (c == '1')
{
g.DrawLine(Pen2, new PointF(x, topY), new PointF(x, topY + h));
x += w;
g.DrawLine(Pen2, new PointF(x, topY), new PointF(x, topY + h));
x += w;
}
}
z = z + 1;
}
#endregion
#region 画结束符
for (int i = 0; i < 4; i++)
{
if (i!= 2)
{

g.DrawLine(Pen1, new PointF(x, t

opY), new PointF(x, topY + h));//根据上边的线条,画在画布上
x += w;
}
else
{
g.DrawLine(Pen2, new PointF(x, topY), new PointF(x, topY + h));//根据上边的线条,画在画布上
x += w;
}
}
#endregion
g.DrawString(s, new Font("宋体", 10), Brushes.Black, new PointF(0, topY + h + 2));
g.Dispose();
bm.Save("11.jpg");
string path = Application.StartupPath + "\\11.jpg";
return path;




}


#region 打印条形码
///


/// 打印条形码
///

public void PrintBarCode()
{

printDialog.ShowHelp = true;
printDialog.AllowSomePages = true;
printDocument.PrintController = new System.Drawing.Printing.StandardPrintController();
if (printDialog.ShowDialog() == DialogResult.OK)//当用户选择打印时,开始打印
{
try
{
printDocument.Print();//开始文档打印进程

//this.printDocument.Print();
}
catch
{
//文档停止打印
printDocument.PrintController.OnEndPrint(printDocument, new System.Drawing.Printing.PrintEventArgs());
}
}
}
///
/// 打印预览
///

public void BarCodeShow()
{
printDialog.ShowDialog();
printPreviewDialog.Document = printDocument;
printPreviewDialog.ShowDialog();
}
///
/// 打印的事件
///

///
///
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{

Image img = Image.FromFile(Application.StartupPath + "\\11.jpg");
e.Graphics.DrawImage(img, new Rectangle(new Point(0, 0), new Size(500, 50)), new RectangleF(new PointF(0, 0), new SizeF(500, 50)), GraphicsUnit.Pixel);

img.Dispose();

}

#endregion
}
}

相关文档
最新文档