コード例 #1
0
        private static IntPtr?_GetHBitmapThumbnail(string fileName, int width, int height, ThumbnailOptions options)
        {
            const int HR_OK = 0;

            var nativeShellItem = Shell32.CreateItemFromName(fileName);

            if (nativeShellItem == null)
            {
                return(null);
            }

            int    hr;
            IntPtr hBitmap;

            try
            {
                var nativeSize = new NativeSize {
                    Width = width, Height = height
                };

                hr = ((IShellItemImageFactory)nativeShellItem)
                     .GetImage(nativeSize, options, out hBitmap);
            }
            finally
            {
                Marshal.ReleaseComObject(nativeShellItem);
            }

            if (hr != HR_OK)
            {
                Log().Information("GetImage for {FileName} failed with {HRESULT}", fileName, hr);
                return(null);
            }

            return(hBitmap);
        }