开发者

How to remove background color of ShellFile “icons”, but not of “real” thumbnails

I’m using WindowsAPICodePack, getting ShellFile’s Thumbnail’s. But some of those which look l开发者_如何学Goike the generic icons – have a black background. I therefore make it a Bitmap and set Black as transparent.

The problem is that when it’s a thumbnail of a picture – it shouldn’t do it. How can I tell a real thumbnail from an “icon”?

My code:

ShellFile sf = ShellFile.FromFilePath(path);
Bitmap bm = sf.Thumbnail.MediumBitmap;
bm.MakeTransparent(Color.Black);

Thanks


You can approach this problem from another angle. It is possible to force the ShellFile.Thumbnail to only extract the thumbnail picture if it exists or to force it to extract the associated application icon.

So your code would look something like this:

Bitmap bm;
using (ShellFile shellFile = ShellFile.FromFilePath(filePath))
{
    ShellThumbnail thumbnail = shellFile.Thumbnail;

    thumbnail.FormatOption = ShellThumbnailFormatOption.ThumbnailOnly;

    try
    {
        bm = thumbnail.MediumBitmap;
    }
    catch // errors can occur with windows api calls so just skip
    {
        bm = null;
    }
    if (bm == null)
    {
        thumbnail.FormatOption = ShellThumbnailFormatOption.IconOnly;
        bm = thumbnail.MediumBitmap;
        // make icon transparent
        bm.MakeTransparent(Color.Black);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜