WPF打印大型报表导致OutOfMemoryException

时间:2012-01-30 11:27:39

标签: .net wpf printing pagination fixeddocument

我创建了一个每月打印的FixedDocument报告。它的平均长度约为350页,大约一半的页面是〜200kb的jpeg图像。报告在DocumentViewer中很好地显示,但在尝试打印时遇到了问题。

使用时

DocumentViewer1.Print()

如果我只是尝试打印月份数据的前三分之一,例如。 120页,它工作正常。 但是,如果我试着整个月,大约在第160页,我得到以下错误

System.OutOfMemoryException: Insufficient memory to continue the execution
of the program.
   at System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(Int32Rect
sourceRect, IntPtr buffer, Int32 bufferSize, Int32 stride)
   at System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(Int32Rect
sourceRect, Array pixels, Int32 stride, Int32 offset)
   at Microsoft.Internal.GDIExporter.CGDIBitmap.Load(BitmapSource pBitmap,
Byte[] buffer, PixelFormat LoadFormat)
   at
Microsoft.Internal.GDIExporter.CGDIRenderTarget.DrawBitmap(BitmapSource
pImage, Byte[] buffer, Rect rectDest)
   at Microsoft.Internal.GDIExporter.CGDIRenderTarget.DrawImage(BitmapSource
source, Byte[] buffer, Rect rect)
   at
Microsoft.Internal.AlphaFlattener.BrushProxyDecomposer.Microsoft.Internal.Al
phaFlattener.IProxyDrawingContext.DrawImage(ImageProxy image, Rect dest,
Geometry clip, Matrix trans)
   at
Microsoft.Internal.AlphaFlattener.PrimitiveRenderer.RenderImage(ImageProxy
image, Rect dest, Geometry bounds, Boolean clipToBounds, Int32 start, Matrix
trans, String desp)
   at
Microsoft.Internal.AlphaFlattener.PrimitiveRenderer.RenderImage(ImageProxy
image, Rect dest, Geometry clip, Matrix trans, String desp)
   at Microsoft.Internal.AlphaFlattener.Flattener.AlphaRender(Primitive
primitive, List`1 overlapping, Int32 overlapHasTransparency, Boolean
disjoint, String desp)
   at
Microsoft.Internal.AlphaFlattener.Flattener.AlphaFlatten(IProxyDrawingContex
t dc, Boolean disjoint)
   at Microsoft.Internal.AlphaFlattener.Flattener.Convert(Primitive tree,
ILegacyDevice dc, Double width, Double height, Double dpix, Double dpiy,
Nullable`1 quality)
   at Microsoft.Internal.AlphaFlattener.MetroDevice0.FlushPage(ILegacyDevice
sink, Double width, Double height, Nullable`1 outputQuality)
   at Microsoft.Internal.AlphaFlattener.MetroToGdiConverter.FlushPage()
   at
System.Windows.Xps.Serialization.NgcSerializationManagerAsync.EndPage()
   at
System.Windows.Xps.Serialization.NgcFixedPageSerializerAsync.EndPersistObjec
tData(Boolean isManualStartDoc)
   at
System.Windows.Xps.Serialization.NgcFixedPageSerializerAsync.AsyncOperation(
NGCSerializerContext context)
   at
System.Windows.Xps.Serialization.NgcSerializationManagerAsync.InvokeSaveAsXa
mlWorkItem(Object arg)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object
source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at
System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object
state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at
System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedClea
nup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32
msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam,
IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object
source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority
priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr
wParam, IntPtr lParam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.Unsaf
eNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,
Int32 reason, Int32 pvLoopData)
   at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(ApplicationContext context)
   at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun(
)
   at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoAppl
icationModel()

然后我认为通过使用以下方式将文档分成小的30页文档,一次只打印30页,我会超越它:

    Dim dlg = New PrintDialog()


    If dlg.ShowDialog() = True Then

        Dim PageCounter As Int32 = 0
        Dim ListOfDocs As New List(Of FixedDocument)
        Dim CurrentFixedDoc As New FixedDocument


        For Each FixedSizePag As FixedPage In PrintListOfPages

            Dim FixedSizedPageConten As PageContent = New PageContent

            CType(FixedSizedPageConten, IAddChild).AddChild(FixedSizePag)
            CurrentFixedDoc.Pages.Add(FixedSizedPageConten)

            PageCounter = PageCounter + 1

            If PageCounter >= 30 Then
                ListOfDocs.Add(CurrentFixedDoc)
                PageCounter = 0
                CurrentFixedDoc = New FixedDocument
            End If

        Next

        If CurrentFixedDoc.Pages.Count > 0 Then
            ListOfDocs.Add(CurrentFixedDoc)
        End If

        For Each docum In ListOfDocs
            dlg.PrintDocument(docum.DocumentPaginator, "Testing - Part " & (ListOfDocs.IndexOf(docum) + 1) & " of " & ListOfDocs.Count)
        Next

    End If

但这只是造成完全相同的错误。我已经尝试从中点和月底打印一百页,这是有效的,所以它必须是页面的体积而不是特定的图像(图像只是来自一个来源的jpegs,最大尺寸400kb)。 / p>

在我看到

的同时打印30页时,观察正在使用的内存量
    Start = 96Mb
    30 pages = 367Mb
    60 pages = 588Mb
    90 pages = 825Mb
    120 pages = 1003Mb
    150 pages = 1238Mb

然后在1281Mb的下一批次崩溃

我在真正的打印机和XPS Windows打印机上进行了测试。当我记下那些记忆值时,我等到每个部分完全假脱机并打印出来。

当我将它们分成单独的单个文档时,我无法理解它为什么会这样构建。我必须处理一些事情,但我不确定是什么?

1 个答案:

答案 0 :(得分:0)

您应该为每个文件生成10个页面(每个文件一个FixedDocument),然后将这些文件合并为一个(您将拥有一个FixedDocumentSequence)。接下来,您将在此FixedDocumentSequence上打印。我确信这个解决方案不会占用太多内存。

System.Windows.Xps.XpsDocumentWriter.Write(System.Windows.Documents.FixedDocumentSequence fixedDocumentSequence)