private void Extract(int width, int height, IEIFLAG flags)
        {
            IntPtr bitmap = IntPtr.Zero;
              SIZE size = new SIZE {
            cx = width,
            cy = height
              };

              try {
            if (_extractImage != null) {
              StringBuilder location = new StringBuilder(260);
              int priority = 0;
              const int requestedColorDepth = 32;

              _hResult = _extractImage.GetLocation(location, location.Capacity, ref priority, ref size, requestedColorDepth, ref flags);
              if (_hResult != S_OK) Marshal.ThrowExceptionForHR(_hResult);

              _hResult = _extractImage.Extract(out bitmap);
              if (_hResult != S_OK) Marshal.ThrowExceptionForHR(_hResult);

              Thumbnail = Image.FromHbitmap(bitmap);
            }
              } catch (COMException) {
              } catch (Exception) {
              } finally {
            if (bitmap != IntPtr.Zero)
              Marshal.Release(bitmap);
              }
        }
        private Bitmap Extract(int width, int height, IEIFLAG flags, bool scale, bool aspect)
        {
            IntPtr zero = IntPtr.Zero;
            SIZE   sIZE = default(SIZE);

            sIZE.cx = width;
            sIZE.cy = height;
            PixelFormat   bgra = PixelFormats.Bgra32;
            BitmapPalette halftone256Transparent = BitmapPalettes.Halftone256Transparent;
            int           num = (width * bgra.BitsPerPixel + 7) / 8;

            byte[]       pixels = new byte[num * height];
            BitmapSource bmpSrc = BitmapSource.Create(width, height, 96.0, 96.0, bgra, halftone256Transparent, pixels, num);

            try
            {
                if (this._extractImage != null)
                {
                    StringBuilder stringBuilder = new StringBuilder(260);
                    int           num2          = 0;
                    this.hResult = this._extractImage.GetLocation(stringBuilder, stringBuilder.Capacity, ref num2, ref sIZE, 32, ref flags);
                    if (this.hResult != 0)
                    {
                        Marshal.ThrowExceptionForHR(this.hResult);
                    }
                    this.hResult = this._extractImage.Extract(out zero);
                    if (this.hResult != 0)
                    {
                        Marshal.ThrowExceptionForHR(this.hResult);
                    }
                    BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(zero, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                    if (scale && (bitmapSource.PixelHeight != sIZE.cy || bitmapSource.PixelWidth != sIZE.cx))
                    {
                        return(this.BmpFromBmpSource(ShellMgr.ScaleImage(bitmapSource, sIZE.cx, sIZE.cy, aspect)));
                    }
                    return(this.BmpFromBmpSource(bitmapSource));
                }
                return(this.BmpFromBmpSource(bmpSrc));
            }
            catch (COMException)
            {
                return(this.BmpFromBmpSource(bmpSrc));
            }
            catch (Exception)
            {
                return(this.BmpFromBmpSource(bmpSrc));
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.Release(zero);
                }
            }
        }
Example #3
0
        private void Extract(int width, int height, IEIFLAG flags)
        {
            IntPtr bitmap = IntPtr.Zero;
            SIZE   size   = new SIZE {
                cx = width,
                cy = height
            };

            try {
                if (_extractImage != null)
                {
                    StringBuilder location            = new StringBuilder(260);
                    int           priority            = 0;
                    const int     requestedColorDepth = 32;

                    _hResult = _extractImage.GetLocation(location, location.Capacity, ref priority, ref size, requestedColorDepth, ref flags);
                    if (_hResult != S_OK)
                    {
                        Marshal.ThrowExceptionForHR(_hResult);
                    }

                    _hResult = _extractImage.Extract(out bitmap);
                    if (_hResult != S_OK)
                    {
                        Marshal.ThrowExceptionForHR(_hResult);
                    }

                    Thumbnail = Image.FromHbitmap(bitmap);
                }
            } catch (COMException) {
            } catch (Exception) {
            } finally {
                if (bitmap != IntPtr.Zero)
                {
                    Marshal.Release(bitmap);
                }
            }
        }
 public Bitmap ExtractImage(int width, int height, IEIFLAG flags, bool scale, bool aspect)
 {
     return(this.Extract(width, height, flags, scale, aspect));
 }
 public Bitmap ExtractImage(int squareWidth, IEIFLAG flags, bool scale, bool aspect)
 {
     return(this.Extract(squareWidth, squareWidth, flags, scale, aspect));
 }
        static Image getThumbnailImageInternal(string filePath, IEIFLAG flags, Size size) {
            IShellFolder desktopFolder = null;
            IShellFolder folderShell = null;
            IExtractImage extract = null;
            object extractObj = null;
            IntPtr folderPidl = IntPtr.Zero;
            int dwAttributes = 0;
            int dwFileAttributes = 0;
            IntPtr filePidl = IntPtr.Zero;
            IntPtr thePath = IntPtr.Zero;
            int chEaten = 0;
            int chFileEaten = 0;
            int prfgInOut = 0;
            IntPtr hBmpThumbnail = IntPtr.Zero;
            int dwPriority = 0;

            var IID_IShellFolder = new Guid("000214E6-0000-0000-C000-000000000046");
            var IID_IExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");

            var folderName = Path.GetDirectoryName(filePath);
            var shortFileName = Path.GetFileName(filePath);

            try {
                SHGetDesktopFolder(out desktopFolder);

                desktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, folderName, ref chEaten, out folderPidl, ref dwAttributes);
                if (folderPidl == IntPtr.Zero) {
                    throw new Exception("fault: ParseDisplayName #1");
                }
                desktopFolder.BindToObject(folderPidl, IntPtr.Zero, ref IID_IShellFolder, ref folderShell);

                if (folderShell == null) {
                    throw new Exception("fault: BindToObject");
                }

                folderShell.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, shortFileName, ref chFileEaten, out filePidl, ref dwFileAttributes);
                if (filePidl == IntPtr.Zero) {
                    throw new Exception("fault: ParseDisplayName #2");
                }

                folderShell.GetUIObjectOf(IntPtr.Zero, 1, ref filePidl, ref IID_IExtractImage, ref prfgInOut, out extractObj);
                if (extractObj == null) {
                    throw new Exception("fault: GetUIObjectOf");
                }

                extract = (IExtractImage)extractObj;

                SIZE prgSize = new SIZE() { cx = size.Width, cy = size.Height };
                StringBuilder pathBuffer = new StringBuilder(MAX_PATH);
                Console.WriteLine(pathBuffer);
                extract.GetLocation(pathBuffer, pathBuffer.Capacity, ref dwPriority, ref prgSize, 32, ref flags);

                hBmpThumbnail = IntPtr.Zero;
                extract.Extract(out hBmpThumbnail);

                if (hBmpThumbnail == IntPtr.Zero) {
                    throw new Exception("fault: Extract");
                }

                Image img = Image.FromHbitmap(hBmpThumbnail);
                Bitmap bmp = new Bitmap(img);
                return bmp;
            }
            catch (Exception ex) {
                throw ex;
            }
            finally {
                if (extract != null) Marshal.ReleaseComObject(extract);
                if (filePidl != IntPtr.Zero) Marshal.FreeCoTaskMem(filePidl);
                if (folderShell != null) Marshal.ReleaseComObject(folderShell);
                if (folderPidl != IntPtr.Zero) Marshal.FreeCoTaskMem(folderPidl);
                if (hBmpThumbnail != IntPtr.Zero) DeleteObject(hBmpThumbnail);
            }
        }