简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM