I'm running into a problem with cpu / memory usage while using Magick.Net in my server application. A single request in itself increases what is used and as more users are on combined with files that contain many pages, it can grind the server to a halt. I wonder if I am missing something in my code, if there is a way to optimize, or if I just need to limit resources.
This process pulls in a collection of byte arrays from another source with each one representing a single page. It then combines them into a single file which is returned as a byte array.
I am currently using Q16 and I'm thinking of using Q8 since it uses less resources, as long as the image quality is still good. In the long run, I may also setup another server to run this process if I can't find another solution.
Code: Select all
private static readonly MagickReadSettings _settings = new MagickReadSettings
{
Density = new Density(300),
UseMonochrome = true,
Compression = CompressionMethod.Group4,
};
public static byte[] CreatePdfFromImageFiles(List<ImageFile> imageFiles)
{
using (MagickImageCollection col = new MagickImageCollection())
{
for (int i = 0; i < imageFiles.Count; i++)
{
byte[] data = imageFiles[i].Bytes;
MagickImage mi = new MagickImage(data, _settings)
{
Format = MagickFormat.Pdf
};
if (imageFiles[i].Rotation != 0)
mi.Rotate(Convert.ToDouble(imageFiles[i].Rotation));
col.Add(mi);
}
return col.ToByteArray();
}
}