带有Crystal Reports的MVC3中的条形码
本文关键字:条形码 MVC3 带有 Reports Crystal | 更新日期: 2023-09-27 18:28:04
技术:Visual Studio 2010和Crystal Reports、MVC3 Web应用程序、多层应用程序。
我的web应用程序构建了一个用于标记打印的报告。该报告工作正常,但无法生成条形码。查看报告时,条形码字段为空。我已经安装了IDAutomationHC39M,并在winforms项目中工作。
生成报告的代码:
public void Barcode()
{
BarCode report = new BarCode();
Barcodes barcodeDetails = new Barcodes();
DataTable dataTable = barcodeDetails._Barcodes;
for (int i = 0; i < 80; i++)
{
DataRow row = dataTable.NewRow();
string nome = "nome" + i;
decimal preco = i;
string barcode = i + "ADF" + i;
row["nome"] = nome;
row["preco"] = preco;
row["barcode"] = barcode;
dataTable.Rows.Add(row);
}
report.Database.Tables["Barcodes"].SetDataSource((DataTable)dataTable);
report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
}
将条形码转换为jpeg是否更容易?如果是,我如何在水晶报告中做到这一点?
EDI I:我尝试更改的代码
for (int i = 0; i < 80; i++)
{
DataRow row = dataTable.NewRow();
string nome = "nome" + i;
decimal preco = i;
string barcode = "*" + i + "ADF" + i + "*";
row["nome"] = nome;
row["preco"] = preco;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barcode.Length * 40, 80))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 10);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + barcode + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
row["barcode"] = imgBarCode.ImageUrl;
}
}
dataTable.Rows.Add(row);
}
但没有效果。
在asp.net应用程序(在我的例子中是MVC)中显示条形码的过程实际上是将字体转换为jpeg。
步骤1:获取条形码的值,并在开始和结束处添加"*",然后创建位图、字体和图形。
DataRow row = dataTable.NewRow();
string barcode = "*" + ID.ToString() + VALOR_VENDA.Value.ToString() + "*";
row["nome"] = NOME_PRODUTO;
row["preco"] = VALOR_VENDA.Value;
int w = barcode.Length * 40;
Bitmap oBitmap = new Bitmap(w, 100);
Graphics oGraphics = Graphics.FromImage(oBitmap);
Font oFont = new Font("IDAutomationHC39M", 18);
PointF oPoint = new PointF(2f, 2f);
SolidBrush oBrushWrite = new SolidBrush(Color.Black);
SolidBrush oBrush = new SolidBrush(Color.White);
oGraphics.FillRectangle(oBrush, 0, 0, w, 100);
oGraphics.DrawString(barcode, oFont, oBrushWrite, oPoint);
接下来使用MemoryStream添加位图。请确保数据集字段的类型为Byte[]。使用内存流将值.ToArray()添加到数据集中;
using (MemoryStream ms = new MemoryStream())
{
oBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] byteImage = ms.ToArray();
row["barcode"] = byteImage;
}
dataTable.Rows.Add(row);