Hi,
wrote code which works, but the bigger the files the more problems I get, how can I optimise my code without running in outofmomory
Code:
` private static void ConvertImagetoPDF(String[] ImageFilenames, string[] directories, string insertName = "", string prefix = "Result", string usersChoise = "1", bool tempLabel = false, double tempHeight = 6000, double tempWidth = 8000)
{
try
{
PdfDocument document = new PdfDocument();
string temp = "";
try
{
Console.WriteLine(globalUsersChoise(ref usersChoise, ref prefix, ref tempHeight, ref tempWidth, ref tempLabel));
//todo: calculate how many pages it can add in one rush
Image[] img = new Image[ImageFilenames.Length];
for (int i = 0; i < ImageFilenames.Length; i++)
//for (int i = 0; i < 4; i++)
{
img[i] = Image.FromFile(ImageFilenames[i]);
// Create an empty page or load existing
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromFile(ImageFilenames[i]);
page.Width = tempWidth;
page.Height = tempHeight;
float width = image.PixelWidth * 0.3f;
float height = image.PixelHeight * 0.3f;
#region create Label
// Create a font
XFont font = new XFont("Times New Roman", 10, XFontStyle.Bold);
// Draw the text
if (tempLabel)
{
gfx.DrawString(Path.GetFileNameWithoutExtension(ImageFilenames[i]), font, XBrushes.Black,
new XRect(120, -30, (page.Width - width), (page.Height - height)),
XStringFormats.Center);
}
#endregion
gfx.DrawImage(image, (page.Width / 2) - (width / 2), (page.Height / 2) - (height / 2), width, height);//put it in the middle
Console.WriteLine((i + 1) + ": " + Path.GetFileName(ImageFilenames[i]));
GC.Collect();//hoped this would do the job
//document.Save(@"..\..\sample" + Path.GetFileNameWithoutExtension(ImageFilenames[i]) + ".pdf");
}
//var temp = @"!" + prefix + DateTime.Now.Ticks.ToString() + ".pdf";
}
finally //will execute this as it is the first exception statement
{ //save the pdf pagedocument
temp = @"!" + prefix + "_" + (insertName.Equals("") ? DateTime.Now.Ticks.ToString() : insertName.ToString().Replace(":", "")) + ".pdf";
document.Save(temp);
document.Close();
Process.Start(temp);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
I hoped cleaning the GarbageCollector would help....
Thank you for supporting
