Results 1 to 3 of 3

Thread: need help with xps to png memory leak

  1. #1
    Hyperactive Member
    Join Date
    Apr 11
    Posts
    268

    need help with xps to png memory leak

    hey all ive been reading some tutorials online on how to convert a xps document to image format fiels (i need this because of a problem displaying a3 files) Anyways heres the full code ive came up with for the printing process:
    Code:
    public void SaveXpsDocumentToImage(string xpsFileName, int ResolutionScaling, bool ColorEnabled, int CopyNumber, Form2 Form2)
                {
                    PrintDocument pd = new PrintDocument();
    
                    maxPageIndex = 0;
                    pd.PrinterSettings.Copies = short.Parse(CopyNumber.ToString());
                    pd.PrinterSettings.Collate = true;
                    pd.DefaultPageSettings.Color = ColorEnabled;
                    pd.DocumentName = xpsFileName;
                    pd.PrinterSettings.DefaultPageSettings.Color = ColorEnabled;
                    if (Form2.PropertiesSettingsPrinter != "")
                        pd.PrinterSettings.PrinterName = Form2.PropertiesSettingsPrinter;
                    pd.DefaultPageSettings.PaperSize = GetPaperSize(pd.PrinterSettings, "A3");
    
                    if (MSList == null)
                        MSList = new List<MemoryStream>();
    
                    XpsDocument xpsDoc = new XpsDocument(Form2.PropertiesSettingsPrintingDirectory + "\\Selected\\" + xpsFileName, System.IO.FileAccess.Read);
                            
                        FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
                        // You can get the total page count from docSeq.PageCount
                        for (int pageNum = 0; pageNum < docSeq.DocumentPaginator.PageCount; ++pageNum)
                        {
                            DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum);
                            RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)docPage.Size.Width * ResolutionScaling, (int)docPage.Size.Height * ResolutionScaling, 96 * ResolutionScaling, // WPF (Avalon) units are 96dpi based
                            96 * ResolutionScaling, System.Windows.Media.PixelFormats.Default);
                            renderTarget.Render(docPage.Visual);
                            PngBitmapEncoder encoder = new PngBitmapEncoder();  // Choose type here ie: JpegBitmapEncoder, etc
                            encoder.Frames.Add(BitmapFrame.Create(renderTarget));
                            MSList.Add(new MemoryStream());
                            encoder.Save(MSList[pageNum]);
                            encoder.Frames.Clear();
                            renderTarget.Clear();
                            docPage.Dispose();
                            maxPageIndex++;
                            renderTarget = null;
                            docPage = null;
                            encoder = null;
                                
                        }
                        pd.PrintPage += new PrintPageEventHandler(PageAdd);
                        currentPageIndex = 0;
                        pd.Print();
                        pd.Dispose();
                        pd = null;
                        docSeq = null;
                        xpsDoc.Close();
                        xpsDoc = null;
                        
                    for (int i = 0; i < MSList.Count(); i++)
                    {
                        MSList[i].SetLength(0);
                        MSList[i].Close();
                        MSList[i].Dispose();
                    }
                    MSList.Clear();
                    GC.Collect();
    
                }
    
                List<MemoryStream> MSList;
                int currentPageIndex = 0, maxPageIndex = 0;
    As you can notice ive tried my best to destroy everything so i could get my ram used by this program back, but from 25mb it goes up by around 20mb each time i print that file, it only has 6 pages and ~500kb on disk.
    Ive no clue what exactly is the problem, i think its obvious to suspect that this ha something to do with the bmp conversion but i cant exactly put my finger on what exactly is causing this.

    This whole function is running on a STA thread
    If you require more information tell me and ill try to post it.
    Any help is appreciated as usual, thanks in advance!
    Last edited by Legendary_Agent; Jun 9th, 2012 at 05:43 AM.

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 09
    Location
    Canada
    Posts
    2,797

    Re: need help with xps to png memory leak

    You might try un-wiring the event handler that you add here when all is said and done:
    Code:
    pd.PrintPage += new PrintPageEventHandler(PageAdd);
    It's possible it is still holding a reference to some memory in code that is not allowing the garbage collector to release it.

  3. #3
    Hyperactive Member
    Join Date
    Apr 11
    Posts
    268

    Re: need help with xps to png memory leak

    Quote Originally Posted by ForumAccount View Post
    You might try un-wiring the event handler that you add here when all is said and done:
    Code:
    pd.PrintPage += new PrintPageEventHandler(PageAdd);
    It's possible it is still holding a reference to some memory in code that is not allowing the garbage collector to release it.
    Thank you for your reply, i will do that and will post back results.

    BTW the problem was a bug in the programming language, the memory leak was caused simply because i didnt do fixedpage.updatelayout(), i will try getting that printpage event out and see if i can get memory back to 20mb, i no longer am experiencing memory leak with the fixedpage.updatelayout but its still up quite some mb as if it was still displaying 1 page.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •