Out of memory Exception while printing in WPF

2019-04-10 11:49发布

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?

0条回答
登录 后发表回答