Skip to content

Commit

Permalink
Addressed memory leaks and parallelized image conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
dusrdev committed Sep 17, 2022
1 parent e98e098 commit 1456e33
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions PdfTool/Controller/ImageToPdfConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using PdfTool.Models;
using PdfSharpCore;
using System.Text;
using PdfTool.Extensions;

namespace PdfTool.Controller;

Expand All @@ -25,32 +26,35 @@ public ImageToPdfConverter(AppSettings settings) {
/// <param name="filePaths"></param>
/// <returns></returns>
public async Task<Result> ConvertImagesAsync(string[] filePaths) {
foreach (string path in filePaths) {
PdfDocument documents = new PdfDocument();
documents.Info.Title = Path.GetFileNameWithoutExtension(path);
var tasks = filePaths.Select(x => new Func<Task>(() => ConvertImageAsync(x)));
await tasks.ParallelForEachAsync(-1, async func => await func());

PdfPage page = documents.AddPage();
XImage image = XImage.FromFile(path);
if (image.PixelWidth > image.PixelHeight) {
page.Orientation = PageOrientation.Landscape;
}
XGraphics gfx = XGraphics.FromPdfPage(page);
return new Result {
Success = true,
Message = "Conversion successful."
};
}

await DrawImage(gfx, image, 0, 0, (int)page.Width, (int)page.Height);
private async Task ConvertImageAsync(string imagePath) {
using var document = new PdfDocument();
document.Info.Title = Path.GetFileNameWithoutExtension(imagePath);

string resultPath = Path.ChangeExtension(path, ".pdf");
var page = document.AddPage();
using var image = XImage.FromFile(imagePath);
if (image.PixelWidth > image.PixelHeight) {
page.Orientation = PageOrientation.Landscape;
}
using var gfx = XGraphics.FromPdfPage(page);

await DrawImage(gfx, image, 0, 0, (int)page.Width, (int)page.Height);

if (File.Exists(resultPath)) {
File.Delete(resultPath);
}
string resultPath = Path.ChangeExtension(imagePath, ".pdf");

if (documents.PageCount > 0) documents.Save(resultPath);
if (File.Exists(resultPath)) {
File.Delete(resultPath);
}

return new Result {
Success = true,
Message = "Conversion successful."
};
if (document.PageCount > 0) document.Save(resultPath);
}

/// <summary>
Expand All @@ -73,22 +77,28 @@ private Task DrawImage(XGraphics gfx, XImage image, int x, int y, int width, int

var inner = image.PixelWidth / (double)image.PixelHeight * area;
var Width = Math.Sqrt(inner);
//var Height = Width * height / width;
var Height = Width * image.PixelHeight / image.PixelWidth;

if (Width > width) {
var ratio = Width / width;
Width /= ratio;
Height /= ratio;
}


if (Height > height) {
var ratio = Height / height;
Height /= ratio;
}

if (Height < height) {
y += (int)((height - Height) / 2);
}

if (Width < width) {
x += (int)((width - Width) / 2);
}

gfx.DrawImage(image, x, y, (int)Width, (int)Height);//scale to A4
return Task.CompletedTask;
}
Expand Down

0 comments on commit 1456e33

Please sign in to comment.