Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# gdi+;从网页保存位图图像时出错_C#_Bitmap_Gdi+_Webpage - Fatal编程技术网

C# gdi+;从网页保存位图图像时出错

C# gdi+;从网页保存位图图像时出错,c#,bitmap,gdi+,webpage,C#,Bitmap,Gdi+,Webpage,我有这样的方法从网页捕获图像。 当DocumentCompleted事件发生时调用此方法。 HTMLDocumentClass只是System.Window.Forms.WebBrowser.Document的一个框架 private bool DoCapture(string strPath, HTMLDocumentClass clsDoc) { WriteLog(" DoCapture 1"); bool bSuc = false;

我有这样的方法从网页捕获图像。 当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中出现了一个通用错误+


我做错了什么,以及如何解决这个问题?

您是否具有将图像保存到的位置的写入权限?可以帮助验证权限。确定。在正常文件打开/写入的情况下,没有问题