【问题标题】:C# Shell Thumbnail doesnt work properly on ppt filesC# Shell Thumbnail 在 ppt 文件上无法正常工作
【发布时间】:2013-09-27 12:30:30
【问题描述】:

我有以下函数来获取文件的BitmapImage

public static BitmapImage GetThumbnail(string filePath)
{
    ShellFile shellFile = ShellFile.FromFilePath(filePath);
    BitmapSource shellThumb = shellFile.Thumbnail.ExtraLargeBitmapSource;

    BitmapImage bImg = new BitmapImage();
    PngBitmapEncoder encoder = new PngBitmapEncoder();
    MemoryStream memoryStream = new MemoryStream();
    encoder.Frames.Add(BitmapFrame.Create(shellThumb));
    encoder.Save(memoryStream);
    bImg.BeginInit();
    bImg.StreamSource = memoryStream;
    bImg.EndInit();

    return bImg;
}

当我得到视频的缩略图时,它总是有效。
当我得到 Presentation (pptx) Thumbnail 时,它不能正常工作(我不知道什么时候可以,什么时候不可以)。

例如,我在目录中有 2 个文件:

这就是它在我的程序中的样子 - 1 可以,1 不可以(有时两者都可以,有时都不行):

如果您能告诉我问题出在哪里,或者给我另一种获取不会失败的缩略图的方法,我将不胜感激......

附言
我想提醒一下,对于视频文件,它可以 100% 正常工作(.mp3、.mp4、.wmv - 这就是我测试过的)

【问题讨论】:

    标签: c# shell thumbnails


    【解决方案1】:

    由于我想获取 pptx 缩略图,我想出了以下解决方案:
    1. 我使用 DotNetZip 提取了缩略图
    2. 我得到了位于 docProps 目录中的缩略图
    3.我把缩略图提取到memoryStream
    4.我将memoryStream转换成BitmapImage并返回。

    代码如下:

    public static BitmapImage GetPPTXThumbnail(string filePath)
    {
        using (ZipFile zip = ZipFile.Read(filePath))
        {
            ZipEntry e = zip["docProps/thumbnail.jpeg"];
    
            BitmapImage bImg = new BitmapImage();
            MemoryStream memoryStream = new MemoryStream();
            bImg.BeginInit();
            e.Extract(memoryStream);
            memoryStream.Seek(0, SeekOrigin.Begin);
            bImg.StreamSource = memoryStream;
            bImg.EndInit();
    
            return bImg;
        }
    }
    

    此解决方案不能失败,此解决方案仅用于获取 PPTX 缩略图,我假设它适用于所有 office xml 文件,例如 docx、xlsx 等...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 2020-02-08
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      相关资源
      最近更新 更多