1

I have such method for capture image from webpage. This method is called when DocumentCompleted event occurs. The HTMLDocumentClass is just a frame of System.Window.Forms.WebBrowser.Document

    private bool DoCapture(string strPath, HTMLDocumentClass clsDoc)
    {
        WriteLog(" DoCapture 1");
        bool bSuc = false;

        Control parent = m_web1.Parent;
        DockStyle dockStyle = m_web1.Dock;
        bool scrollbarsEnabled = m_web1.ScrollBarsEnabled;

        if (parent != null)
        {
            parent.Controls.Remove(m_web1);
        }

        Rectangle screen = Screen.PrimaryScreen.Bounds;
        Rectangle body = m_web1.Document.Body.ScrollRectangle;
        if (clsDoc != null)
        {
            IHTMLElement2 ele = (IHTMLElement2)clsDoc.body;
            body = new Rectangle(0, 0, ele.scrollWidth, ele.scrollHeight);
        }

        Rectangle docRectangle = new Rectangle()
        {
            Location = new Point(0, 0),
            Size = new Size(
                body.Width > screen.Width ? body.Width : screen.Width,
                body.Height > screen.Height ? body.Height : screen.Height)
        };
        //set the width and height of the WebBrowser object
        m_web1.ScrollBarsEnabled = false;
        m_web1.Size = new Size(docRectangle.Width, docRectangle.Height);

        Rectangle imgRectangle;
        imgRectangle = docRectangle;
        Bitmap bitmap = new Bitmap(imgRectangle.Width, imgRectangle.Height);
        IViewObject viewObject = m_web1.Document.DomDocument as IViewObject;

        using (Graphics g = Graphics.FromImage(bitmap))
        {
            IntPtr hdc = g.GetHdc();
            viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, ref imgRectangle, ref docRectangle, IntPtr.Zero, 0);
            g.ReleaseHdc(hdc);
        }

        if (parent != null)
        {
            parent.Controls.Add(m_web1);
            m_web1.Dock = dockStyle;
        }
        m_web1.ScrollBarsEnabled = scrollbarsEnabled;
        if (bitmap != null)
        {   
            bSuc = true;
            try
            {
                bitmap.Save(strPath, ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                WriteLog("  PATH:" + strPath + ":" + ex.Message);
            }
            bitmap.Dispose();
        }
        WriteLog(" DoCapture 2");
        return bSuc;
    }

But, I got A generic error occurred in GDI+

what i do wrong, and how to solve this?

  • Do you have the write permissions for the location you are saving your image to? [This topic](http://stackoverflow.com/questions/1410127/c-sharp-test-if-user-has-write-access-to-a-folder) can help in verifying permissions. – Yurii Dec 20 '13 at 08:14
  • Sure. In case of normal file open/write, there is no problem – user3121960 Dec 20 '13 at 10:22

0 Answers0