从 WebBrowser 控件保存图像

发布于 2024-11-05 10:08:17 字数 914 浏览 1 评论 0原文

运行以下代码,但生成的位图向下移动约半英寸并在底部截止。我检查了图像的宽度和高度,它正在创建正确尺寸的图像,只是图像内容向下移动并被截断。我很困惑...有什么想法吗?

    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;
    }

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

私藏温柔 2024-11-12 10:08:17

您所做的就是渲染图像,就像浏览器以所有样式渲染图像一样。不知道这是否是你真正想要的?如果您只想下载图像,那么使用网络请求来解决它会更容易,如其他答案中所述。

如果您确实想要渲染,第一步就是更改

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

为“

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

现在您将获得完整渲染的网络浏览器图像”。

如果您想要一个完美的解决方案,即使是大图像,您也必须滚动并逐块获取图像。

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.

花开浅夏 2024-11-12 10:08:17

首先,我相信您获得的 img 元素的大小与实际图像大小不同。

其次,为什么不直接使用 System.Net.WebRequest 并从 URL 下载实际图像。您已经有了 URL 并且已经有了 IMG 元素信息,事实上,如果您不显示网络浏览器,请尝试使用 System.Net.WebRequest,这样您就可以验证您获得的内容类型是实际图像还是占位符。

http://msdn.microsoft.com/en-us/library /system.net.webrequest.aspx

to start with I believe the img element you getting has a different size other than what is the actual image size.

Secondly, why don't you use direct System.Net.WebRequest and download the actual image from the URL. you already have the URL and Already have the IMG element information, infact if you not show the webbrowser try using System.Net.WebRequest, this way you can verify what content type you getting is it an actual image or place holder.

http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx

眼眸 2024-11-12 10:08:17

如果您有地址,可以使用以下方式保存:

client.DownloadFile

当客户端是 System.Net.WebClient

If you have the address, you can save it using:

client.DownloadFile

When client is a System.Net.WebClient

三岁铭 2024-11-12 10:08:17
Imports System.Net
Imports System.Runtime.InteropServices
Imports mshtml
--Add reference Microsoft Html Object Library

Sub Dowork()
Dim x = WebBrowser1.Document.GetElementsByTagName("img")
For i As Integer = 0 To x.Count - 1
If x(i).GetAttribute("alt") = "Captcha image" Then
GetImage(x(i)).Save("captcha.png", Imaging.ImageFormat.Png)
End If
Next
End Sub

<ComImport>
<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
<Guid("3050F669-98B5-11CF-BB82-00AA00BDCE0B")>
Public Interface IHTMLElementRenderFixed
Sub DrawToDC(hdc As IntPtr)
Sub SetDocumentPrinter(bstrPrinterName As String, hdc As IntPtr)
End Interface




Public Function GetImage(e As HtmlElement) As Bitmap
    Dim img As IHTMLImgElement = TryCast(e.DomElement, IHTMLImgElement)
    Dim render As IHTMLElementRenderFixed = TryCast(img, IHTMLElementRenderFixed)
    Dim bmp As Bitmap = New Bitmap(e.OffsetRectangle.Width, e.OffsetRectangle.Height)
    Dim g As Graphics = Graphics.FromImage(bmp)
    Dim hdc As IntPtr = g.GetHdc()
    render.DrawToDC(hdc)
    g.ReleaseHdc(hdc)
    Return bmp
End Function
Imports System.Net
Imports System.Runtime.InteropServices
Imports mshtml
--Add reference Microsoft Html Object Library

Sub Dowork()
Dim x = WebBrowser1.Document.GetElementsByTagName("img")
For i As Integer = 0 To x.Count - 1
If x(i).GetAttribute("alt") = "Captcha image" Then
GetImage(x(i)).Save("captcha.png", Imaging.ImageFormat.Png)
End If
Next
End Sub

<ComImport>
<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
<Guid("3050F669-98B5-11CF-BB82-00AA00BDCE0B")>
Public Interface IHTMLElementRenderFixed
Sub DrawToDC(hdc As IntPtr)
Sub SetDocumentPrinter(bstrPrinterName As String, hdc As IntPtr)
End Interface




Public Function GetImage(e As HtmlElement) As Bitmap
    Dim img As IHTMLImgElement = TryCast(e.DomElement, IHTMLImgElement)
    Dim render As IHTMLElementRenderFixed = TryCast(img, IHTMLElementRenderFixed)
    Dim bmp As Bitmap = New Bitmap(e.OffsetRectangle.Width, e.OffsetRectangle.Height)
    Dim g As Graphics = Graphics.FromImage(bmp)
    Dim hdc As IntPtr = g.GetHdc()
    render.DrawToDC(hdc)
    g.ReleaseHdc(hdc)
    Return bmp
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文