2

我创建了一个每月打印的 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

但这只是导致完全相同的错误。我已经尝试从中点到月底打印一百页,这很有效,所以它必须是页面的数量而不是特定的图像(图像只是 jpeg 全部来自一个来源,最大大小 400kb)。

我看到一次打印 30 页时正在使用的内存量

    Start = 96Mb
    30 pages = 367Mb
    60 pages = 588Mb
    90 pages = 825Mb
    120 pages = 1003Mb
    150 pages = 1238Mb

然后在下一批以 1281Mb 崩溃

我已经在一台真正的打印机和 XPS windows 打印机上进行了测试。当我记下这些内存值时,我一直等到每个部分都完全假脱机并打印出来。

我不明白为什么当我将它们分成单独的单独文档时它只是这样构建。我必须处理一些东西,但我不确定是什么?

4

1 回答 1

0

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

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

于 2013-08-07T11:38:45.640 回答