ImageMagick.NET - Better Performance

I am using the ImageMagick.NET library for C # and I want to get some information from every page in a .PDF document. Here is my current code:

var list = new MagickImageCollection(); list.Read(file.FullName); foreach (var page in list) { if (!backgroundWorker.CancellationPending) { pageCount.pageColorspace(page); isFormat(page.Width, page.Height); pageCount.incPdfPages(); } } 

But in my opinion, performance is very slow. 10 PDF files with 703 pages take 4 minutes. Is there a way to speed it up?

+5
source share
1 answer

You can improve performance by reading page after page. If you read the entire file, there will be 703 pages in memory. Your machine may not allocate as much memory, and ImageMagick will use a disk to store pixels, which will decrease performance.

You can specify the page you want to read using the FrameIndex property of the MagickReadSettings class. If you specify a page that is too high, an exception will be added (Magick.NET 7.0.0.0005 or higher is required) with a message stating that you are requesting an invalid page. You need to do this because ImageMagick does not know the number of pages of a PDF file. The following is an example of how you could do this.

 int page = 0; while (true) { MagickReadSettings settings = new MagickReadSettings() { FrameIndex = page }; try { using (MagickImage image = new MagickImage(@"C:\YourFile.pdf", settings)) { // Do something with the image.... } } catch (MagickException ex) { if (ex.Message.Contains("Requested FirstPage is greater")) break; else throw; } page++; } 
+2
source

Source: https://habr.com/ru/post/1202868/


All Articles