从WebBrowser控件保存图像 [英] Saving images from a WebBrowser Control

查看:374
本文介绍了从WebBrowser控件保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code运行,但所产生的位图下移约半英寸,并切断底部。我检查了图像的宽度和高度,这是创建正确大小的图像,只是图像内容都将向下移动并切断。我难倒...任何想法?

 使用MSHTML;
    使用System.Drawing中;
    使用了System.Runtime.InteropServices;

    [ComImport,InterfaceType((短)1),与GUID(3050F669-98B5-11CF-BB82-00AA00BDCE0B)]
    专用接口IHTMLElementRenderFixed
    {
        无效DrawToDC(IntPtr的HDC);
        无效SetDocumentPrinter(字符串bstrPrinterName,IntPtr的HDC);
    }

    公共位图的getImage(串号)
    {
        的HtmlElement E = webBrowser1.Document.GetElementById(ID);
        IHTMLImgElement IMG =(IHTMLImgElement)e.DomElement;
        IHTMLElementRenderFixed渲染=(IHTMLElementRenderFixed)IMG;

        BMP位图=新位图(img.width,img.height);
        图形G = Graphics.FromImage(BMP);
        IntPtr的HDC = g.GetHdc();
        render.DrawToDC(HDC);
        g.ReleaseHdc(HDC);

        返回BMP;
    }
 

解决方案

你在做什么的渲染图像,因为它是由浏览器与所有的风格呈现。我不知道这是不是你真的想要什么?如果你只是想下载的图像,然后很容易与Web请求在兴田其他答案说明解决这个问题。

如果你真的想要呈现比第一步骤是修改

  BMP位图=新位图(img.width,img.height);
 

  BMP位图=新位图(e.OffsetRectangle.Width,e.OffsetRectangle.Height);
 

现在你得到完整呈现网页浏览器的图像。

如果你想即使是大图像的完美解决方案,您还可以左右滚动和瓷砖获得图像控件。

The following code runs, but the Bitmap generated is shifted down about half an inch and cutoff at the bottom. I checked the image width and height and it is creating an image of the correct size, just the image contents are shifted down and cutoff. I'm stumped... any ideas?

    using mshtml;
    using System.Drawing;
    using System.Runtime.InteropServices;

    [ComImport, InterfaceType((short)1), Guid("3050F669-98B5-11CF-BB82-00AA00BDCE0B")]
    private interface IHTMLElementRenderFixed
    {
        void DrawToDC(IntPtr hdc);
        void SetDocumentPrinter(string bstrPrinterName, IntPtr hdc);
    }

    public Bitmap GetImage(string id)
    {
        HtmlElement e = webBrowser1.Document.GetElementById(id);
        IHTMLImgElement img = (IHTMLImgElement)e.DomElement;
        IHTMLElementRenderFixed render = (IHTMLElementRenderFixed)img;

        Bitmap bmp = new Bitmap(img.width, img.height);
        Graphics g = Graphics.FromImage(bmp);
        IntPtr hdc = g.GetHdc();
        render.DrawToDC(hdc);
        g.ReleaseHdc(hdc);

        return bmp;
    }

解决方案

What you are doing is rendering the image as it is rendered by the browser with all the styles. I don't know if this is what your realy want? If you only want to download the image then it is easier to solve it with a web request as described in hte other answers.

If you realy want to render than the first step is to change

Bitmap bmp = new Bitmap(img.width, img.height);

to

Bitmap bmp = new Bitmap(e.OffsetRectangle.Width, e.OffsetRectangle.Height);

Now you get the complete rendered web browser image.

If you want a perfect solution even for big images you also have to scroll around and get the image tile by tile.

这篇关于从WebBrowser控件保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆