前几天项目上用到了水晶报表的条码打印功能
最开始用的字体,code39 code128都试了,可以正常打印,但是扫描枪不识别,
据说code39值两边加*可以扫描到,我没能成功
又在网上找到以下方法,可以打印和扫描。
大致思路是:用一个程序文件生成条码图片(此例规则是Code39,参数Code是要生成条码的值),
然后在报表页面加入一个图片控件,再用该图片控件动态抓取条码图片,显示到报表控件上。
Code39Handler.ashx
就是生成条码图片的文件,参数是Code,加到项目里面,直接访问文件就能看到生成的条码图片,
例如:http://localhost/..../Code39Handler.ashx?Code=123456
代码:
using System;using System.Web;using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Text;/// <summary>/// 用 .NET 繪圖 API,搭配條碼最普遍的 Code 39 編碼規則 (一般超商的讀條碼機都可讀),產生條碼圖檔/// </summary>public class Code39Handler : IHttpHandler {public void ProcessRequest (HttpContext context) {//context.Response.ContentType = "text/plain";//context.Response.Write("Hello World");//Logic to retrieve the image file//context.Response.ContentType = "image/jpeg";//context.Response.WriteFile("MyImage01.jpg");string mycode = context.Request["code"];string 字串;string 字元;//字串 = "*-%$*"字串 = "*" + mycode + "*"; //Code 39 的特性是前、後置碼會標識「星號(*)」,表示開始和結束int 畫布高 = 35;int 畫布寬 = 0;int 筆x = 0;int 筆y = 20;//int 筆寬 = 0;if (!string.IsNullOrEmpty(mycode)){畫布寬 = 字串.Length * 13;Bitmap BMP = new Bitmap(畫布寬, 畫布高, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);Graphics G = Graphics.FromImage(BMP);G.TextRenderingHint = TextRenderingHint.AntiAlias;G.Clear(Color.White);Brush 筆刷1 = new SolidBrush(Color.White);G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;G.FillRectangle(筆刷1, 0, 0, 畫布寬, 畫布高);for (int i = 0; i < 字串.Length; i++){//取得 Code 39 碼的規則字元 = this.genBarcode(字串.Substring(i, 1).ToUpper());for (int j = 0; j < 4; j++){if (字元.Substring(j, 1).Equals("0")){G.DrawLine(Pens.Black, 筆x, 0, 筆x, 筆y);}else{G.DrawLine(Pens.Black, 筆x, 0, 筆x, 筆y);G.DrawLine(Pens.Black, 筆x + 1, 0, 筆x + 1, 筆y);筆x += 1;}筆x += 1;if (字元.Substring(j + 5, 1).Equals("0")){G.DrawLine(Pens.White, 筆x, 0, 筆x, 筆y);}else{G.DrawLine(Pens.White, 筆x, 0, 筆x, 筆y);G.DrawLine(Pens.White, 筆x + 1, 0, 筆x + 1, 筆y);筆x += 1;}筆x += 1;} //end of loopif (字元.Substring(4, 1).Equals("0")){G.DrawLine(Pens.Black, 筆x, 0, 筆x, 筆y);}else{G.DrawLine(Pens.Black, 筆x, 0, 筆x, 筆y);G.DrawLine(Pens.Black, 筆x + 1, 0, 筆x + 1, 筆y);筆x += 1;}筆x += 2;} //end of loopint x = 0;int addx = 13;G.DrawString("-", new Font("Arial", 10, FontStyle.Italic), SystemBrushes.WindowText, new PointF(x, 20));x += addx;for (int k = 0; k < mycode.Length; k++){G.DrawString(mycode.Substring(k, 1), new Font("Arial", 10, FontStyle.Italic), SystemBrushes.WindowText, new PointF(x, 20));x = x + addx;}G.DrawString("-", new Font("Arial", 10, FontStyle.Italic), SystemBrushes.WindowText, new PointF(x, 20));BMP.Save(context.Response.OutputStream, ImageFormat.Jpeg);G.Dispose();BMP.Dispose();}else{畫布寬 = 100;Bitmap BMP = new Bitmap(畫布寬, 畫布高, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);Graphics G = Graphics.FromImage(BMP);G.TextRenderingHint = TextRenderingHint.AntiAlias;G.Clear(Color.White);//未給參數時顯示的提示內容G.DrawString("無條碼產生", new Font("宋体", 12, FontStyle.Regular), SystemBrushes.WindowText, new PointF(0, 20));BMP.Save(context.Response.OutputStream, ImageFormat.Jpeg);G.Dispose();BMP.Dispose();}}// 規則可參考網址 1:/xuzhongxuan/archive//05/28/2489358.aspx// 規則可參考網址 2:/zryou/blog/static/690318471704226450//// <summary>/// Code 39 碼的規則。/// Code 39 碼可使用的字元如下:0~9、A~Z、+、-、*、/、%、$、. 及空白字元。 /// </summary>/// <param name="code"></param>/// <returns></returns>public string genBarcode(string code){switch (code){case "0":code = "001100100";break;case "1":code = "100010100";break;case "2":code = "010010100";break;case "3":code = "110000100";break;case "4":code = "001010100";break;case "5":code = "101000100";break;case "6":code = "011000100";break;case "7":code = "000110100";break;case "8":code = "100100100";break;case "9":code = "010100100";break;case "A":code = "100010010";break;case "B":code = "010010010";break;case "C":code = "110000010";break;case "D":code = "001010010";break;case "E":code = "101000010";break;case "F":code = "011000010";break;case "G":code = "000110010";break;case "H":code = "100100010";break;case "I":code = "010100010";break;case "J":code = "001100010";break;case "K":code = "100010001";break;case "L":code = "010010001";break;case "M":code = "110000001";break;case "N":code = "001010001";break;case "O":code = "101000001";break;case "P":code = "011000001";break;case "Q":code = "000110001";break;case "R":code = "100100001";break;case "S":code = "010100001";break;case "T":code = "001100001";break;case "U":code = "100011000";break;case "V":code = "010011000";break;case "W":code = "110001000";break;case "X":code = "001011000";break;case "Y":code = "101001000";break;case "Z":code = "011001000";break;case "*":code = "001101000";break;case "-":code = "000111000"; //好像辨識不出來break;case "%":code = "100101000"; //好像辨識不出來break;case "$":code = "010101000"; //好像辨識不出來break;default:code = "010101000"; //都不是就印 $break;}return code;}public bool IsReusable {get {return false;}}}
把文件加到项目里,打开加好图片控件的报表,右击图片控件,选择[设置对象格式]---[图片]选项卡,最下面有[图形位置]一栏,打开, 下面最大空白框里就写刚才访问Code39Handler.ashx的路径就行。到这里基本就完成了,也可以有很多补充的。。。。。。
注意:
vs自带的报表图片控件是没有【图形位置】这一项的。
一定要把报表装到vs里,否则不显示条码,会显示原图片
CrystalReports 下载链接,用浏览器下载老会断...何解?? 用迅雷下载就好好的
https://smpdl.sap-ag.de/~sapidp/01200252310000998949E/cr_sp3_fullbuild.zip
密钥: CFKOA-YOTTM2M-OOUFAFF-N43M 注册号: 6707437608
原文链接:/WizardWu/archive//09/14/1825523.html