2013-08-16 38 views
1

技術:Visual Studio 2010和Crystal Reports,MVC3 Web應用程序,多層應用程序。帶有Crystal Reports的MVC3中的條碼

我的網絡應用程序構建一個報告標籤打印。報告工作正常,但不生成條形碼。當我查看報告時,條形碼字段爲空。我已經安裝了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我:我試着改變代碼:

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); 
      } 

但沒有效果。

+0

我不願意問,但是您是否將水晶報表字段設置爲使用您安裝的IDAutomationHC39M字體? – ChargerIIC

+0

這種方法僅適用於winforms,因爲字體安裝在本地機器上。在web應用程序中,條形碼是在服務器中創建的,只能在客戶端上可視化。 – Ramalho

回答

0

在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 []類型。使用memorystream向數據集添加值.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);