Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 like 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

like image 376
ispiro Avatar asked Oct 02 '11 12:10

ispiro


1 Answers

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);
    }
}
like image 130
Kev Avatar answered Nov 19 '22 00:11

Kev