Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows API Code Pack - ShellFile not generating PDF bitmap

Using the code from previous stack overflow questions:

System.Drawing.Bitmap image;
ShellFile f = ShellFile.FromFilePath(fileLocation);
image = f.Thumbnail.ExtraLargeBitmap;
image.Save(tempfile, ImageFormat.Png);

I am trying to use window API to get the thumbnail of a PDF

I am led to believe that this generates an image file that resembles the first page of the PDF document.

The reality however is that it does NOT look like that, and merely looks like the PDF icon.

Is there anything that I'm missing that is required before this actually works as intended?

PDF files are correctly associated with adobe reader.

When browsing directories in windows explorer I DO see thumbnails associated with the documents.

I should note that the code DOES in fact correctly extract thumbnails when dealing with Excel and Word documents.

EDIT (references):

  • C# get thumbnail from file via windows api
  • Get thumbnail of any file, not only image files on Windows XP/Vista
  • Windows API Code Pack Thumbnail gives preview thumb of pdf but not Word or Excel
like image 720
Alex C Avatar asked Aug 15 '13 21:08

Alex C


2 Answers

You need to specify that you want the thumbnail, not the icon (the default). Change your code into this:

System.Drawing.Bitmap image;
ShellFile f = ShellFile.FromFilePath(fileLocation);

//force the actual thumbnail, not the icon
f.Thumbnail.FormatOption = ShellThumbnailFormatOption.ThumbnailOnly;

image = f.Thumbnail.ExtraLargeBitmap;
image.Save(tempfile, ImageFormat.Png);
like image 138
Tudor Olariu Avatar answered Oct 20 '22 06:10

Tudor Olariu


The problem is because you have not selected the active frame that you will create the thumbnail from.

I cannot verify it on my current machine, because I don't have the Windows API on it, but it's giving you the standard PDF thumbnail because in your code you have't specified which page to use for the thumbnail.

Try doing something like this:

        Image image = new Image();
        //Load image here
        int frameindex = 1; // Page number you want to use for thumbnail
        Guid guide = image.FrameDimensionsList[0];
        FrameDimension fDimension = new FrameDimension(guide);
        image.SelectActiveFrame(fDimension, frameindex);
        //Then go on to extract your thumbnail
like image 43
astallaslions Avatar answered Oct 20 '22 05:10

astallaslions