Out of memory Exception while printing in WPF

谁都会走 提交于 2019-12-21 17:48:11

问题


I was trying to print the set of 70 images in WPF. So I used Fixed document as I saw in many references and tried printing using the below code.

private void button1_Click(object sender, RoutedEventArgs e)
    {
        PrintDialog d = new PrintDialog();
        d.PrintDocument(PrintingDoc().DocumentPaginator, "test");
    }

    private FixedDocument PrintingDoc()
    {
        FixedDocument document = new FixedDocument();
        Visual viewerControl;
        string[] Documents = System.IO.Directory.GetFiles("../../U/");
        DrawingVisual dv;
        DrawingContext context ;
        BitmapImage im ;
        foreach (string doc in Documents)
        {

                dv = new DrawingVisual();
                context = dv.RenderOpen();
                im = new BitmapImage();
                im.BeginInit();
                im.UriSource = new Uri(doc, UriKind.Relative);
                im.EndInit();
                context.DrawImage(im, new Rect(0,0,im.Width,im.Height));
                context.Close();

                PageContent m_PageContent = new PageContent();
                FixedPage page = new FixedPage();

                VisCont myContainer = new VisCont();
                myContainer.AddVisual(dv);
                page.Children.Add(myContainer);
                ((IAddChild)m_PageContent).AddChild(page);
                document.Pages.Add(m_PageContent);
            }
        }
        return document;
    }
}
internal class VisCont: FrameworkElement
{
    private readonly VisualCollection children;
    public VisCont()
    {
        children = new VisualCollection(this);
    }

    public void AddVisual(Visual v)
    {
        children.Add(v);
    }
}

I got the below exception in d.PrintDocument "Insufficient memory to continue the execution of the program."

and for the note, this is reproduce in X86 configuration only and not in X64. Any help friends?

来源:https://stackoverflow.com/questions/18872636/out-of-memory-exception-while-printing-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!