4

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;
    }
toby
  • 885
  • 3
  • 10
  • 21

4 Answers4

3

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.

Matthias Alleweldt
  • 2,453
  • 17
  • 16
  • Would scrolling really be necessary for large images? My WebBrowser control is not even visible. I would think that rendering it from the graphics object would work whether the image is in view or not. I will need to experiment with that some. – toby May 05 '11 at 16:05
  • I use the renderer for complete pages or big HTML tables. There I need to scroll. – Matthias Alleweldt May 05 '11 at 16:08
  • In my case, web request access is denied for image url, maybe security reason. I can get general html page from webrequest, but image url. – JHK Sep 05 '21 at 05:42
2

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

Sanj
  • 3,770
  • 6
  • 26
  • 31
  • 1
    I would rather not download the image again since it's already loaded in the WebControl. Maybe I can get it from the cache? If all else fails I will use a WebRequest and download it again. Thanks. – toby May 05 '11 at 14:52
  • In my case, web request access is denied for image url, maybe security reason. I can get general html page from webrequest, but image url. – JHK Sep 05 '21 at 05:42
1
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
  • Could you explain how/why your code works? That'll enable the OP and others to understand and apply your methods (where applicable) elsewhere. Code-only answers are [discouraged](http://meta.stackexchange.com/q/148272/284827) and liable to be deleted. — [During review](http://stackoverflow.com/review/late-answers/11706009) – Wai Ha Lee Mar 20 '16 at 21:34
  • worked for me... many thanks – Behnam Faghih Jul 28 '23 at 10:44
1

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

client.DownloadFile

When client is a System.Net.WebClient

  • In my case, web request access is denied for image url, maybe security reason. I can get general html page from webrequest, but image url. – JHK Sep 05 '21 at 05:42