1

技术:Visual Studio 2010 和 Crystal Reports、MVC3 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);
            }

但是没有效果。

4

1 回答 1

0

在 asp.net 应用程序(在我的例子中是 MVC)中显示条形码的过程实际上是在 jpeg 中转换字体。

第一步:获取条形码的值,在开头和结尾添加“*”,然后创建位图、字体和图形。

                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);
于 2013-08-17T16:49:05.383 回答