0

I use WebView2 on my VB.Net Window Form and have been tring to copy an image on a navigated website to clipboard and save the image as bitmap to local folder. I googled a lot for last three days but unfortunately can not find a right solution. Below is one of the codes I tried.

Private Sub Button1_Click() Handles Button1.Click
    WebView2.Navigate("https://www.microsoft.com/en-us/")
End Sub

Private Async Sub WebView2_NavigationCompleted() Handles WebView2.NavigationCompleted     
    Await WebView2.CoreWebView2.ExecuteScriptAsync("document.querySelector('picture img.lazyloaded')[1].select();")
    Await WebView2.CoreWebView2.ExecuteScriptAsync("document.execCommand('copy');")
    Using bmp As Bitmap = DirectCast(Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)
        bmp.Save("C:\Users\Me\Downloads\test1.bmp")
    End Using
End Sub

Because I do not know about javascript much, that may results in unsuccessful code. Anyhow I tried lots of code but still can not achieve to copy image displayed on WebView2. I would appreciate for your help. Other than ExecuteScriptAsync, bitmap (directly save in jpg etc) are very welcome. I only know that javascript is the only method to handle WebView2 so far.

Tom
  • 123
  • 1
  • 1
  • 7
  • I looked at the link, I suggest, you grap the 'src' link for the image, then use a HttpClient to download the image. – Poul Bak Jul 15 '20 at 21:23
  • I do not use HttpClient but WebView2 to save image. I need to be done by WebView2. – Tom Jul 15 '20 at 23:10
  • See this: https://stackoverflow.com/questions/33321095/cannot-use-document-execcommandcopy-from-developer-console – Poul Bak Jul 15 '20 at 23:19
  • I tried "document.exexCommand('copy'); and it resulted in True (successful) on Console of Google Chrome. – Tom Jul 16 '20 at 01:27
  • I tried to insert a screen shot of the successful code. But sorry I can not insert the screen shot. – Tom Jul 16 '20 at 01:42

0 Answers0