简体   繁体   English

从 WebBrowser 控件保存图像

[英]Saving images from a WebBrowser Control

The following code runs, but the Bitmap generated is shifted down about half an inch and cutoff at the bottom.以下代码运行,但生成的 Bitmap 向下移动了大约半英寸并在底部截断。 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.您正在做的是渲染图像,因为它由浏览器渲染所有 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.如果您只想下载图像,那么使用 web 请求更容易解决,如其他答案中所述。

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.现在您获得了完整渲染的 web 浏览器图像。

If you want a perfect solution even for big images you also have to scroll around and get the image tile by tile.如果您想要一个完美的解决方案,即使是大图像,您也必须滚动并逐块获取图像。

to start with I believe the img element you getting has a different size other than what is the actual image size.首先,我相信您获得的 img 元素的大小与实际图像大小不同。

Secondly, why don't you use direct System.Net.WebRequest and download the actual image from the URL.其次,为什么不直接使用System.Net.WebRequest并从 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.您已经拥有 URL 并且已经拥有 IMG 元素信息,事实上,如果您不显示网络浏览器,请尝试使用 System.Net.WebRequest,这样您就可以验证您获得的内容类型是实际图像还是占位符。

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

If you have the address, you can save it using:如果您有地址,可以使用以下方法保存:

client.DownloadFile

When client is a System.Net.WebClient当客户端是System.Net.WebClient

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM