public Bitmap Load(string path, Size size)
        {
            if (path == null)
            {
                return(null);
            }

            if (UrlHelper.IsUrl(path))
            {
                return(null);
            }

            if (imageCache.ContainsKey(path))
            {
                return(imageCache[path]);
            }

            using (Image img = ImageHelper2.SafeGetEmbeddedThumbnail(path) ?? ImageHelper2.SafeFromFile(path))
            {
                if (img == null)
                {
                    imageCache.Add(path, null);
                }
                else
                {
                    ImageHelper.AutoRotateFromExifOrientation(img);
                    imageCache.Add(path, ResizeImage((Bitmap)img, size.Width, size.Height));
                }
            }
            return(imageCache[path]);
        }
        private static Bitmap GetElementPreviewImage(IHTMLDocument3 doc3, IHTMLElement element, int snapshotWidth, int snapshotHeight)
        {
            try
            {
                // @RIBBON TODO: Need to make this work for RTL as well.
                IDisplayServices displayServices = ((IDisplayServices)doc3);

                element.scrollIntoView(true);

                tagPOINT offset = new tagPOINT();
                offset.x = 0;
                offset.y = 0;
                displayServices.TransformPoint(ref offset, _COORD_SYSTEM.COORD_SYSTEM_CONTENT, _COORD_SYSTEM.COORD_SYSTEM_GLOBAL, element);

                using (Bitmap snapshotAfter = HtmlScreenCaptureCore.TakeSnapshot((IViewObject)doc3, snapshotWidth, snapshotHeight))
                {
                    //snapshotAfter.Save(@"c:\temp\snapshot" + element.id + ".bmp");

                    Rectangle elementRect;
                    elementRect = new Rectangle(Math.Max(2, offset.x), 2, Math.Min(element.offsetWidth, element.offsetParent.offsetWidth), element.offsetHeight);

                    if (element.offsetWidth <= 0 || element.offsetHeight <= 0)
                    {
                        return(null);
                    }

                    Bitmap cropped = ImageHelper2.CropBitmap(snapshotAfter, elementRect);
                    //cropped.Save(@"c:\temp\snapshot" + element.id + ".cropped.bmp");
                    return(cropped);
                }
            }
            catch (Exception ex)
            {
                Trace.Fail("Failed to get element preview image for id " + element.id + ": " + ex);
                return(null);
            }
        }
            void Save(Stream stream, ImageFormat format)
            {
                if (format == ImageFormat.Gif)
                {
                    if ((bitmap.PixelFormat & PixelFormat.Indexed) != PixelFormat.Indexed && bitmap.Palette.Entries.Length == 0 && !ImageHelper2.IsAnimated(bitmap))
                    {
                        OctreeQuantizer quantizer = new OctreeQuantizer(255, 8);
                        bitmap = quantizer.Quantize(bitmap);
                    }

                    /*
                     * ColorPalette p = bitmap.Palette;
                     * bool seenTransparent = false;
                     * foreach (Color c in p.Entries)
                     * {
                     *  if (c.A == 0)
                     *  {
                     *      Trace.Assert(!seenTransparent);
                     *      seenTransparent = true;
                     *  }
                     * }
                     */
                }

                bitmap.Save(stream, format);
            }
        private static Bitmap ResizeImage(Bitmap image, int maxWidth, int maxHeight)
        {
            Size resize = ImageHelper2.ImageResizer.ScaledResize(image, new Size(maxWidth, maxHeight));

            return(ImageHelper2.CreateResizedBitmap(image, resize.Width, resize.Height, ImageFormat.Jpeg));
        }