This is a migrated thread and some comments may be shown as answers.

CreateThumbnail in Background

3 Answers 309 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Veteran
Kevin asked on 25 Sep 2020, 12:07 PM

Good day,

how is it possible to create the pdf thumbnail images in background without blocking the whole application?

I am generating a list of thumbnails of the 1st page of many PDFS, which results in the application UI beeing blocked.

This is what I am doing:

public ImageSource ConvertPdfToImageThumbnail(string filePath)
        {
            var tf = new ThumbnailFactory();
 
            using Stream stream = File.Open(filePath, FileMode.Open);
            var formatProvider = new PdfFormatProvider();
            RadFixedDocument document = formatProvider.Import(stream);
 
            if (document.Pages.Count > 0)
            {
                ImageSource result = tf.CreateThumbnail(document.Pages[0], document.Pages[0].Size);
                return result;
            }
 
            return null;
        }

 

The function is not doing anything on the UI, just loading a PDF and creating the ImageSource.

When called from a different Thread,  the line

    "ImageSource result = tf.CreateThumbnail(document.Pages[0], document.Pages[0].Size);"

is throwing this error: "the calling thread must be STA ..."

How can this task be done without blocking the UI?

Thank you for your support!

3 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 29 Sep 2020, 12:49 PM

Hi Kevin,

Yes, I was able to reproduce this. The ThumnailFactory uses UI elements to render the image and this requires using the main UI thread. What you can do is to create a new thread and perform the conversion there: 

Thread thread = new Thread(() =>
{
    var image = ConvertPdfToImageThumbnail(@"..\..\SampleDoc.pdf");
    SaveImage(image);
});

thread.SetApartmentState(ApartmentState.STA);
thread.Start();

If you have any other questions, please do not hesitate to contact us.

Regards,
Dimitar
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).

0
Kevin
Top achievements
Rank 1
Veteran
answered on 29 Sep 2020, 03:13 PM

Hi Dimitar,

Thank you very much, this works great!

For anyone else stumbling accross this issue: One last thing that has to be done is calling img.Freeze(); before assigning it to the view property (before the image gets assigned between the 2 threads).

Regards,

Kevin

0
Dimitar
Telerik team
answered on 30 Sep 2020, 05:14 AM

Hi Kevin,

I am glad that this works for your case. And thanks for sharing the solution.

If you have any questions, please do not hesitate to contact us.

Regards,
Dimitar
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).

Tags
PdfProcessing
Asked by
Kevin
Top achievements
Rank 1
Veteran
Answers by
Dimitar
Telerik team
Kevin
Top achievements
Rank 1
Veteran
Share this question
or