public HtmlScreenCaptureForm(HtmlScreenCaptureCore htmlScreenCaptureCore)
        {
            // save reference to parent object
            _htmlScreenCaptureCore = htmlScreenCaptureCore;

            // set the timeout time
            _timeoutTime = DateTime.Now.AddMilliseconds(_htmlScreenCaptureCore.TimeoutMs);

            // create and add the underlying browser control
            _browserControl        = new ExplorerBrowserControl();
            _browserControl.Silent = true;

            Controls.Add(_browserControl);
        }
        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);
            }
        }
        public HtmlScreenCaptureForm(HtmlScreenCaptureCore htmlScreenCaptureCore)
        {
            // save reference to parent object
            _htmlScreenCaptureCore = htmlScreenCaptureCore;

            // set the timeout time
            _timeoutTime = DateTime.Now.AddMilliseconds(_htmlScreenCaptureCore.TimeoutMs);

            // create and add the underlying browser control
            _browserControl = new ExplorerBrowserControl();
            _browserControl.Silent = true;

            Controls.Add(_browserControl);
        }
        private void timer_Tick(object sender, EventArgs e)
        {
            if (reentrant)
            {
                return;
            }
            reentrant = true;

            Timer timer = null;

            try
            {
                timer = sender as Timer;
                IHTMLDocument2 document = _browserControl.Document as IHTMLDocument2;

                // make sure the document is ready
                if (!DocumentReady(document))
                {
                    if (TimedOut)
                    {
                        CleanupAndExit(timer);
                    }

                    return;
                }

                if (Ids != null)
                {
                    IHTMLDocument3 doc3 = document as IHTMLDocument3;

                    foreach (string elementId in Ids)
                    {
                        IHTMLStyle3 style3 = (IHTMLStyle3)(doc3.getElementById(elementId)).style;
                        style3.wordWrap = "normal";
                    }

                    int originalWidth  = _browserControl.Width;
                    int originalHeight = _browserControl.Height;
                    foreach (string elementId in Ids)
                    {
                        try
                        {
                            IHTMLElement element = doc3.getElementById(elementId);

                            _browserControl.Height = element.offsetHeight + 2 + 2;

                            element.scrollIntoView(true);

                            Padding padding = HTMLElementHelper.PaddingInPixels(element);

                            Color backgroundColor = HTMLColorHelper.GetBackgroundColor(element, true, null, Color.White);

                            using (Bitmap elementBitmap = GetElementPreviewImage(doc3, element, _browserControl.Width, _browserControl.Height))
                            {
                                _htmlScreenCaptureCore.SetElementCaptureProperties(elementId, elementBitmap, backgroundColor, padding);
                            }
                        }
                        catch (Exception ex)
                        {
                            Trace.Fail("Failed to capture element " + elementId + ": " + ex);
                        }
                    }

                    // Restore the browser control size
                    _browserControl.Width  = originalWidth;
                    _browserControl.Height = originalHeight;
                }



                // fire event to see if the Bitmap is ready
                using (Bitmap bitmap = HtmlScreenCaptureCore.TakeSnapshot((IViewObject)_browserControl.Document, _browserControl.Width, _browserControl.Height))
                {
                    HtmlScreenCaptureAvailableEventArgsCore ea = new HtmlScreenCaptureAvailableEventArgsCore(bitmap);
                    _htmlScreenCaptureCore.FireHtmlScreenCaptureAvailable(ea);

                    if (ea.CaptureCompleted)
                    {
                        // provide the bitmap to our parent object
                        _htmlScreenCaptureCore.SetCapturedBitmap(bitmap);
                        //bitmap.Save(@"c:\temp\captured.bmp");

                        // exit
                        CleanupAndExit(timer);
                    }
                    else if (TimedOut) // if we have timed out then exit
                    {
                        CleanupAndExit(timer);
                    }

                    // otherwise just let the Timer call us again
                    // at the next interval...
                }
            }
            catch (Exception ex)
            {
                _htmlScreenCaptureCore.SetException(ex);
                CleanupAndExit(timer);
            }
            finally
            {
                reentrant = false;
            }
        }
 /// <summary>
 /// Initialize a screen capture for a snippet of HTML content.
 /// </summary>
 /// <param name="htmlContent">HTML snippet to capture.</param>
 /// <param name="contentWidth">Width of content.</param>
 public HtmlScreenCapture(string htmlContent, int contentWidth)
 {
     _htmlScreenCapture = new HtmlScreenCaptureCore(htmlContent, contentWidth);
     SubscribeToEvents();
 }
 /// <summary>
 /// Initialize a screen capture for an HTML page located at a URL.
 /// </summary>
 /// <param name="url">Url of HTML page to capture.</param>
 /// <param name="contentWidth">Width of content.</param>
 public HtmlScreenCapture(Uri url, int contentWidth)
 {
     _htmlScreenCapture = new HtmlScreenCaptureCore(url, contentWidth);
     SubscribeToEvents();
 }
        private static Color GetBackgroundColorFromPixel(IHTMLElement e)
        {
            IHTMLElement divElement = (e.document as IHTMLDocument2).createElement("div");
            divElement.id = Guid.NewGuid().ToString();
            divElement.style.padding = "10px 10px 10px 10px";
            divElement.style.width = "100px";
            divElement.style.height = "100px";
            (e as IHTMLDOMNode).appendChild((IHTMLDOMNode)divElement);
            string documentHtml = HTMLDocumentHelper.HTMLDocToString(e.document as IHTMLDocument2);
            (e as IHTMLDOMNode).removeChild((IHTMLDOMNode)divElement);

            HtmlScreenCaptureCore screenCapture = new HtmlScreenCaptureCore(documentHtml, 800);
            DocumentSnapshotHandler snapshotHandler = new DocumentSnapshotHandler(divElement.id);
            screenCapture.HtmlDocumentAvailable += new HtmlDocumentAvailableHandlerCore(snapshotHandler.HtmlDocumentAvailable);
            Bitmap docImage = screenCapture.CaptureHtml(3000);
            using (Graphics g = Graphics.FromImage(docImage))
            {
                Rectangle rect = snapshotHandler.rect;
                g.DrawRectangle(new Pen(Color.Blue, 1), rect);
            }
            using (FileStream fs = new FileStream(@"c:\temp\docImage.png", FileMode.Create))
            {
                docImage.Save(fs, ImageFormat.Png);
            }

            Color c = docImage.GetPixel(snapshotHandler.rect.X + 5, snapshotHandler.rect.Y + 5);
            return c;
        }