1

I have build a winform application which connects a technical drawing application (CAD - SolidEdge) and an ERP-system. This is working fine but I can't get the right thumbnails in the bomstructure.

When I click on a file in Windows (Windows 10) I see a nice preview image. How can I extract this image to my application?

I had found a similar question and solution (Extract thumbnail for any file in Windows) but this is won't work anymore (because of Windows 10 updates I guess).

Also this one (C# get thumbnail from file via windows api) doesn't work and gives: Example wrong thumbnail and Example wrong thumbnail.

Have you guys any idea how to solve this? Thanks!!

T.S.
  • 18,195
  • 11
  • 58
  • 78
Marinus
  • 13
  • 4

1 Answers1

0

There are different types of thumbnails that can be retrieved from Windows.

  1. Picture
  2. Song Album Art
  3. Document Icon
  4. Folder
  5. File Group
  6. Single Item

Microsoft has a nice sample project called FileThumbnails that lets you play with each type. This project was updated for Windows10 and VS 2019 in March 2020. Although it's a universal windows project instead of winforms.

After playing with the different modes I found the one you are after for Solid Edge files is #6.


internal class FileExtensions
{
    public static readonly string[] SEfiles    = new string[] { ".dft", ".par", ".asm" };
}

FileOpenPicker openPicker = new FileOpenPicker();
foreach (string extension in FileExtensions.SEfiles)
{
    openPicker.FileTypeFilter.Add(extension);
}

StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
    const ThumbnailMode thumbnailMode = ThumbnailMode.SingleItem;

    bool fastThumbnail = FastThumbnailCheckBox.IsChecked.Value;
    ThumbnailOptions thumbnailOptions = ThumbnailOptions.UseCurrentScale;
    if (fastThumbnail)
    {
        thumbnailOptions |= ThumbnailOptions.ReturnOnlyIfCached;
    }

    using (StorageItemThumbnail thumbnail = await file.GetScaledImageAsThumbnailAsync(thumbnailMode, size, thumbnailOptions))
    {
        if (thumbnail != null)
        {
            MainPage.DisplayResult(ThumbnailImage, OutputTextBlock, thumbnailMode.ToString(), size, file, thumbnail, false);
        }
        else
        {
            rootPage.NotifyUser(Errors.NoThumbnail, NotifyType.StatusMessage);
        }
    }
}

public static void DisplayResult(Image image, TextBlock textBlock, string thumbnailModeName, uint size, IStorageItem item, StorageItemThumbnail thumbnail, bool isGroup)
{
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.SetSource(thumbnail);
    image.Source = bitmapImage;

    textBlock.Text = String.Format("ThumbnailMode.{0}\n"
                                       + "{1} used: {2}\n"
                                       + "Requested size: {3}\n"
                                       + "Returned size: {4}x{5}",
                                       thumbnailModeName,
                                       isGroup ? "Group" : item.IsOfType(StorageItemTypes.File) ? "File" : "Folder",
                                       item.Name,
                                       size,
                                       thumbnail.OriginalWidth,
                                       thumbnail.OriginalHeight);
    }

Result Example:

enter image description here

Automate This
  • 30,726
  • 11
  • 60
  • 82