diff --git a/Windows.Shell.Common/ShellObjects/ShellItem.cs b/Windows.Shell.Common/ShellObjects/ShellItem.cs index c4901740..c120b71f 100644 --- a/Windows.Shell.Common/ShellObjects/ShellItem.cs +++ b/Windows.Shell.Common/ShellObjects/ShellItem.cs @@ -695,7 +695,7 @@ public class ShellItem : IComparable, IDisposable, IEquatableOne or more of the option flags. /// The resulting image. /// - public SafeHBITMAP GetImage(SIZE size, ShellItemGetImageOptions flags) => Images.GetImageAsync(size, flags).Result; + public SafeHBITMAP GetImage(SIZE size, ShellItemGetImageOptions flags) => Images.GetImage(size, flags); /// Gets a property description list object given a reference to a property key. /// diff --git a/Windows.Shell.Common/ShellObjects/ShellItemImages.cs b/Windows.Shell.Common/ShellObjects/ShellItemImages.cs index 37a2043b..f91ba2d4 100644 --- a/Windows.Shell.Common/ShellObjects/ShellItemImages.cs +++ b/Windows.Shell.Common/ShellObjects/ShellItemImages.cs @@ -27,9 +27,9 @@ public class ShellItemImages /// If set to , ignore the use post vista interfaces like . /// The resulting image. /// - public async Task GetImageAsync(SIZE size, ShellItemGetImageOptions flags = 0, bool forcePreVista = false) => await TaskAgg.Run(() => + public SafeHBITMAP GetImage(SIZE size, ShellItemGetImageOptions flags = 0, bool forcePreVista = false) { - SafeHBITMAP hbmp = null; + SafeHBITMAP hbmp = SafeHBITMAP.Null; HRESULT hr = HRESULT.E_FAIL; var sz = (uint)size.Width; if (!forcePreVista && ShellItem.IsMinVista) @@ -47,19 +47,20 @@ public class ShellItemImages } // If before Vista, or if Vista interfaces failed, try using IExtractImage and IExtractIcon - if (hr != HRESULT.S_OK && !flags.IsFlagSet(ShellItemGetImageOptions.IconOnly)) - hr = LoadImageFromExtractImage(shellItem.Parent.IShellFolder, shellItem.PIDL.LastId, ref sz, out hbmp); - if (hr != HRESULT.S_OK) + var isf = shellItem.Parent?.IShellFolder; + if (hr != HRESULT.S_OK && !flags.IsFlagSet(ShellItemGetImageOptions.IconOnly) && isf is not null) + hr = LoadImageFromExtractImage(isf, shellItem.PIDL.LastId, ref sz, out hbmp); + if (hr != HRESULT.S_OK && isf is not null) { if (!flags.IsFlagSet(ShellItemGetImageOptions.ThumbnailOnly)) { - LoadIconFromExtractIcon(shellItem.Parent.IShellFolder, shellItem.PIDL.LastId, ref sz, out SafeHICON hIcon).ThrowIfFailed(); + LoadIconFromExtractIcon(isf, shellItem.PIDL.LastId, ref sz, out SafeHICON hIcon).ThrowIfFailed(); using (hIcon) hbmp = hIcon.ToHBITMAP(); } else { - throw hr.GetException(); + throw hr.GetException()!; } } @@ -75,5 +76,16 @@ public class ShellItemImages } return hbmp; - }, System.Threading.CancellationToken.None); + } + + /// + /// Gets an image that represents this item. The default behavior is to load a thumbnail. If there is no thumbnail for the current + /// item, it retrieves the icon of the item. The thumbnail or icon is extracted if it is not currently cached. + /// + /// A structure that specifies the size of the image to be received. + /// One or more of the option flags. + /// If set to , ignore the use post vista interfaces like . + /// The resulting image. + /// + public async Task GetImageAsync(SIZE size, ShellItemGetImageOptions flags = 0, bool forcePreVista = false) => await TaskAgg.Run(() => GetImage(size, flags, forcePreVista), System.Threading.CancellationToken.None); } \ No newline at end of file