2013-10-30 32 views
0
private MemoryStream ConvertWebChartChartToImage(WebChartControl chart) 
{ 
    using (var pcl = new PrintableComponentLink(new PrintingSystem()) 
    { 
     PageHeaderFooter = new PageHeaderFooter(new PageHeaderArea(new string[] { "A", "Header" }, 
      SystemFonts.DialogFont, BrickAlignment.Center), 
      new PageFooterArea(new string[] { "B" }, 
       SystemFonts.DialogFont, BrickAlignment.Center)), 
     Component = ((IChartContainer)chart).Chart, 
     Landscape = true 
    }) 
    { 
     ((Chart)pcl.Component).OptionsPrint.SizeMode = DevExpress.XtraCharts.Printing.PrintSizeMode.Stret ch; 

     TransDistributionWCh.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Right; 
     pcl.CreateDocument(); 
     var stream = new MemoryStream(); 
     pcl.PrintingSystem.ExportToPdf(stream); 
     return stream; 
    } 

} 
    private void ConvertHTMLStringToPDF() 
    { 
     using (var stream = new MemoryStream()) 
     { 
      var listChartControl = new List<WebChartControl>(new List<WebChartControl> 
      { 
       SuccTransDistributionWCh, 
       AmountPerDayWCh, 
       TransPerDayWCh, 
       AmountPerTransPerDayWCh, 
       ActiveTerminalPerDayWCh, 
       TransNoWCh, 
       TransAmountWCh, 
       TransNoAmountWCh 
      }); 
      foreach (var item in listChartControl) 
      { 

       var temp = ConvertWebChartChartToImage(item); 
       stream.Write(temp.ToArray(), 0, temp.ToArray().Length); 

      } 
      HttpContext.Current.Response.ContentType = "application/pdf"; 
      HttpContext.Current.Response.AddHeader("Accept-Header", stream.Length.ToString(CultureInfo.InvariantCulture)); 
      HttpContext.Current.Response.AddHeader("Content-Disposition", ("Attachment") + "; filename=chart.pdf"); 
      HttpContext.Current.Response.AddHeader("Content-Length", stream.Length.ToString(CultureInfo.InvariantCulture)); 
      HttpContext.Current.Response.ContentEncoding = Encoding.Default; 
      HttpContext.Current.Response.BinaryWrite(stream.ToArray()); 
     } 
     HttpContext.Current.Response.End();} 

我是使用的網絡控制圖dev的表達 ,需要網絡控制圖表轉換爲PDF 我的問題:如何將數據添加到內存流? 此代碼顯示最後一個網絡圖表 我建議零號碼錯誤 stream.Write(temp.ToArray(),0,temp.ToArray()。Length); 搜索谷歌與本網站不幸的是問題沒有解決如何添加內存流

回答

0

我也沒辦法,ExportToPdf方法是如何工作的,但如果它是由人類,會有足夠使用單個流:

private void ConvertWebChartChartToImage(WebChartControl chart, Stream stream) 
{ 
    // ... 
    pcl.PrintingSystem.ExportToPdf(stream); 
} 

private void ConvertHTMLStringToPDF() 
{ 
    using (var stream = new MemoryStream()) 
    { 
     // ... 
     foreach (var item in listChartControl) 
     { 
      ConvertWebChartChartToImage(item, stream); 
     } 
     // ... 
    } 
} 

注,你原來的代碼導致不必要的內存分配:

stream.Write(
    temp.ToArray(), // allocate temporary array, copy stream content into array 
    0, 
    temp.ToArray().Length // allocate another array, copy stream content into array 
    ); 

,不處置MemoryStream例如,從ConvertWebChartChartToImage方法返回。

此外,如果要將一個Stream的內容複製到另一個Stream,則有CopyTo/CopyToAsync方法。