Windows API Code Pack - ShellFile not generating PDF bitmap
Asked Answered
Z

3

6

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):

Zara answered 15/8, 2013 at 21:40 Comment(2)
Same problem here on Windows 2008R2. Without Adobe Reader installed, I got just a blank document thumbnail. After installing Adobe Reader AND rebooting, I get the Adobe PDF Thumbnail indicating the file type. However on my developer box which is Windows 7, it works fine for me and gives me the proper thumbnail.Appetency
Have you found a solution on your own? I am facing the same Issue, but independendt of the data format. It would be nice if you could share your codePrimine
S
2

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);
Spriggs answered 23/3, 2014 at 14:0 Comment(0)
G
1

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
Gibert answered 15/8, 2013 at 22:20 Comment(1)
Hmm... sadly that didn't seem to work. Thanks for contributing though.Zara
K
0

I was not able to get ExtraLargeBitmap to work for PDF files but all other sizes (Large, Medium, and Small) worked fine.

Dim MyShellFile As ShellFile = ShellFile.FromFilePath(fi.FullName)
Dim MyThumbNail As Image
MyShellFile.Thumbnail.FormatOption = ShellThumbnailFormatOption.ThumbnailOnly
MyThumbNail = MyShellFile.Thumbnail.LargeBitmap
Me.PictureBox2.Image = MyThumbNail
Kansu answered 19/11, 2014 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.