如何追加内存流

时间:2013-10-30 14:07:11

标签: c# memorystream

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

我使用web控制图开发快递     并需要将Web控制图转换为pdf     我的问题:如何将数据添加到内存流?     此代码显示上一个Web图表     我建议零号错了     stream.Write(temp.ToArray(),0,temp.ToArray()。Length);     搜索谷歌和这个网站不幸的问题没有解决

1 个答案:

答案 0 :(得分: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个方法。

相关问题