C# Windows窗体超过700页的打印问题

C# Windows窗体超过700页的打印问题,c#,winforms,C#,Winforms,我有一些自定义的Windows窗体,动态加载每个窗体,并使用System.Drawing.printing.PrintDocument打印窗体控制值 我的问题是,我有700多页要打印,如果打印超过300页我的内存不足,如何使用System.Drawing打印700多页。PrintingPrintDocument具有OnBeginPrint(),您可以在继承的类中重写它(或作为订阅它的事件处理). 它通过一个参数,该参数可以告诉您此打印是用于预览还是实际打印(实际上有3个值:PrintToFile

我有一些自定义的Windows窗体,动态加载每个窗体,并使用
System.Drawing.printing.PrintDocument
打印窗体控制值


我的问题是,我有700多页要打印,如果打印超过300页我的内存不足,如何使用
System.Drawing打印700多页。Printing

PrintDocument具有OnBeginPrint(),您可以在继承的类中重写它(或作为订阅它的事件处理). 它通过一个参数,该参数可以告诉您此打印是用于预览还是实际打印(实际上有3个值:PrintToFilePrintToPreviewPrintToPrinter)。您可以通过这种方式区分打印的目的。此外,您需要知道的唯一一件事是,当您预览文档时,您的OnPrintPage()将被连续调用700次,从而在内存中构建整个文档;当您实际将文档打印到打印机上时,在调用打印页面上的后,它将不会在内存中保留以前的所有页面。这就是为什么打印任何文档时,实际上只需要内存来存储其内存最为繁忙的页面

因此,您只需在预览时绘制简化页面内容,并仅在实际打印时完整绘制页面

下一个示例说明了我的方法:

class PrintDocument1 : PrintDocument
{
    int currentPage;
    bool isPreview;

    protected override void OnBeginPrint(PrintEventArgs e)
    {
        currentPage = 0;
        isPreview = e.PrintAction == PrintAction.PrintToPreview;
    }

    protected override void OnPrintPage(PrintPageEventArgs e)
    {
        currentPage++;

        if (isPreview)
        {
            // simplified page drawing

            e.Graphics.FillRectangle(Brushes.BurlyWood, e.MarginBounds);
            e.Graphics.DrawString("Page #" + currentPage, SystemFonts.CaptionFont, Brushes.Black, e.MarginBounds.Location);
        }
        else
        {
            // full page drawing

            Bitmap[] bmaps = new Bitmap[] {
                SystemIcons.Application.ToBitmap(),
                SystemIcons.Information.ToBitmap(),
                SystemIcons.Question.ToBitmap(),
                SystemIcons.Warning.ToBitmap(),
                SystemIcons.Shield.ToBitmap(),
                SystemIcons.Error.ToBitmap()
            };

            Point p = e.MarginBounds.Location;
            for (int i = 0; p.Y < e.MarginBounds.Bottom; i++, p.Y += 40)
            {
                e.Graphics.DrawString("Some text goes here, line " + (i + 1), SystemFonts.CaptionFont, Brushes.Black, p);
                e.Graphics.DrawLine(Pens.CadetBlue, e.MarginBounds.Left, p.Y, e.MarginBounds.Right, p.Y);
                for (int j = 0; j < bmaps.Length; j++)
                    e.Graphics.DrawImage(bmaps[j], p.X + 200 + j * 40, p.Y);
            }
        }

        e.HasMorePages = currentPage < 700; // a lot of pages to draw
    }
}

// above class can be previewed and printed next way:
// PrintPreviewDialog d = new PrintPreviewDialog();
// d.Document = new PrintDocument1();
// d.ShowDialog();
类PrintDocument1:PrintDocument
{
int当前页面;
bool-isPreview;
受保护的覆盖无效OnBeginPrint(PrintEventArgs e)
{
currentPage=0;
isPreview=e.PrintAction==PrintAction.PrintToPreview;
}
PrintPage上受保护的覆盖无效(PrintPageEventArgs e)
{
currentPage++;
如果(isPreview)
{
//简图
e、 图形.圆角矩形(画笔.粗木,e.边缘边界);
e、 Graphics.DrawString(“Page#”+currentPage,SystemFonts.CaptionFont,brush.Black,e.MarginBounds.Location);
}
其他的
{
//整页图纸
位图[]bmaps=新位图[]{
SystemIcons.Application.ToBitmap(),
SystemIcons.Information.ToBitmap(),
SystemIcons.Question.ToBitmap(),
SystemIcons.Warning.ToBitmap(),
SystemIcons.Shield.ToBitmap(),
SystemIcons.Error.ToBitmap()
};
点p=e.边界位置;
对于(int i=0;p.Y
谁将阅读700页?除非你正在打印一本小说,否则这个问题可能是一个设计问题,因为打印那么多的页面会杀死地球:S。这些页面可能是在内存中处理的,所以答案是添加更多内存。是的。我知道,700页需要更多的内存,但我们要求连续打印700页以上,每个表单都有很多控件,我们是否有其他打印选项?您的打印代码可能没有在应该的位置处理某些资源。您必须向我们显示您的打印代码。最有可能的是,您没有处理资源。肯定是缺少使用或处理问题。PrintPage事件处理程序将创建大量System.Drawing对象,但不会分配足够的内存来触发GC。使用任务管理器进行诊断,添加“GDI对象”以查看未分散对象的数量。