Search code examples
c#imagedownloadmshtml

C# - Download picture loaded by mshtml


I have to get avatar picture from a website without ask credentials to user of my app. Just one pre-requisites, user memorize his credentials in Internet Explorer before running app.

So, I open an Internet Explorer and navigate using mshtml and SHDocVw. (Login not asked because memorized)

I can navigate to an url with the only content is the picture, but can't find how to download it.

Finded a mshtml.HTMLImg in my HtlmDocument but can't retrieve stream of the picture.

Some one have the solution ?

Thanks in advance for help.

Regards.

EDIT: Find something like that to put image data in clipboard

// avatar is my mshtml.HTMLImg

mshtml.IHTMLElement2 body2 = (mshtml.IHTMLElement2)doc.body;

mshtml.IHTMLControlRange controlRange = (mshtml.IHTMLControlRange)body2.createControlRange();

controlRange.add((mshtml.IHTMLControlElement)avatar);

controlRange.execCommand("Copy", false, System.Reflection.Missing.Value); controlRange.remove(0);

But this code is blocking on the exec command ...no error but stuck in this line.

(Info: Internet explorer 10 is used)


Solution

  • hi here is a one function I am using to copy captcha images form webbrowser control without navigating to image url or refersh page ,You need to change the image parameters as per your need

    U have not specified any html tag for Avatar so I am unable to edit following function which exactly suit ur need,

    Private Function copyImageToClipBoard() As String
        Dim doc As mshtml.IHTMLDocument2 = DirectCast(wc.Document.DomDocument, mshtml.IHTMLDocument2)
        Dim imgRange As mshtml.IHTMLControlRange = DirectCast(DirectCast(doc.body, mshtml.HTMLBody).createControlRange(), mshtml.IHTMLControlRange)
    
        For Each img As mshtml.IHTMLImgElement In doc.images
            imgRange.add(DirectCast(img, mshtml.IHTMLControlElement))
            If img.src.Contains("recaptcha/api/image?c=") Or img.src.Contains("seccode.php") Then
                imgRange.execCommand("Copy", False, Nothing)
                If isManual = True Then
                    Using bmp As Bitmap = DirectCast(Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)
                        Dim ofrmCap As New frmCaptcha(bmp)
                        ofrmCap.BringToFront()
                        ofrmCap.ShowDialog()
                        mCaptcha = vars.mCaptcha
                        vars.mCaptcha = Nothing
                    End Using
                Else
                    bgwDecaptcher.RunWorkerAsync(Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
                End If
            End If
    
        Next
        Return mCaptcha
    End Function