public void aspnet_barcode()
{
HttpContext c = new HttpContext(Request, Response);
//获取生成条形码的高度
if (c.Request.QueryString["height"] != null)
{
_height = Convert.ToInt32(c.Request.QueryString["height"].ToString());
}
//获取生成条形码的编号
if (c.Request.QueryString["barcode"] != null)
{
}
//调用自定义BarCode方法,打印出条形码
barcode = BarCode("*"+Session["receipt"].ToString ()+"*", 1);
//barcode = BarCode(Label1.Text, 1);
int p_l = barcode.Length;
int p_h = _height ;
//输出生成条形码的图片
c.Response.ContentType = "image/gif";
Bitmap myBitmap = new Bitmap(p_l, p_h);
//创建一个画布
Graphics myGrap = Graphics.FromImage(myBitmap);
//清空背景色
myGrap.Clear(Color.White);
for (int i = 0; i < p_l; i++)
{
//创建画笔对象
Pen myPen = new Pen(Color.White, 1);
if (barcode.Substring(i, 1) == "|")
{
myPen.Color = Color.Black;
}
//绘制一条连接由坐标对指定的两点连线
myGrap.DrawLine(myPen, i, 0, i, _height);
}
//myGrap.DrawString(Session["receipt"].ToString(), new Font("Courier New", 10), new SolidBrush(Color.Black), -4, _height);
//将此图像以指定格式保存到数据流中
myBitmap.Save(c.Response.OutputStream, ImageFormat.Gif);
string savePath = Server.MapPath("Receipt/" + Session["receipt"] + ".jpg");
if (!System.IO.File.Exists(savePath))
{
myBitmap.Save(savePath);
c.Response.End();
}
}
public static string BarCode(object str, int type_code)
{
#region
string strTmp = str.ToString();
string code = strTmp;
strTmp = strTmp.ToLower();
strTmp = strTmp.Replace("0", "_|_|__||_||_|"); ;
strTmp = strTmp.Replace("1", "_||_|__|_|_||");
strTmp = strTmp.Replace("2", "_|_||__|_|_||");
strTmp = strTmp.Replace("3", "_||_||__|_|_|");
strTmp = strTmp.Replace("4", "_|_|__||_|_||");
strTmp = strTmp.Replace("5", "_||_|__||_|_|");
strTmp = strTmp.Replace("7", "_|_|__|_||_||");
strTmp = strTmp.Replace("6", "_|_||__||_|_|");
strTmp = strTmp.Replace("8", "_||_|__|_||_|");
strTmp = strTmp.Replace("9", "_|_||__|_||_|");
strTmp = strTmp.Replace ("*","_|__|_||_||_|");
if (type_code == 1)
{
return strTmp + "<BR>" + code;
}
else
{
return strTmp;
}
#endregion
}
C# 制作39条形码
最新推荐文章于 2021-06-28 10:54:56 发布