|
-
Jun 8th, 2012, 05:56 PM
#1
Thread Starter
Hyperactive Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|