我有这样的方法来从网页捕获图像。 发生DocumentCompleted事件时调用此方法。 HTMLDocumentClass只是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;
}
但是,我在GDI +中发生了一般错误
我做错了什么,以及如何解决这个问题?