【发布时间】:2019-05-16 10:27:36
【问题描述】:
我正在使用 WebBrowser 控件导航到 Google 图片。目的是能够右键单击任何图像并下载并填充 PictureBox 背景。
我有自己的ContextMenuStrip,上面有复制,并且禁用了内置的上下文菜单。
我遇到的问题是从CurrentDocument.MouseMove 返回的坐标总是相对于第一张(左上角)图像。
因此,如果我想要的图像是页面上的第一个图像,我的代码可以正常工作,但是单击任何其他图像总是返回第一个图像的坐标。
看起来坐标是相对于每个图像而不是页面的。
Private WithEvents CurrentDocument As HtmlDocument
Dim MousePoint As Point
Dim Ele As HtmlElement
Private Sub Google_covers_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.IsWebBrowserContextMenuEnabled = False
WebBrowser1.ContextMenuStrip = ContextMenuStrip1
End Sub
Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
CurrentDocument = WebBrowser1.Document
End Sub
Private Sub CurrentDocument_MouseMove(sender As Object, e As HtmlElementEventArgs) Handles CurrentDocument.MouseMove
MousePoint = New Point(e.MousePosition.X, e.MousePosition.Y)
Me.Text = e.MousePosition.X & " | " & e.MousePosition.Y
End Sub
Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
Ele = CurrentDocument.GetElementFromPoint(MousePoint)
If Ele.TagName = "IMG" Then
CopyToolStripMenuItem.Visible = True
Else
CopyToolStripMenuItem.Visible = False
End If
End Sub
Private Sub CopyToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CopyToolStripMenuItem.Click
Dim ToImg = Ele.GetAttribute("src")
mp3_row_edit.PictureBox1.BackgroundImage = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData(ToImg)))
ToImg = Nothing
End Sub
【问题讨论】:
-
谢谢,但即使使用他们的代码,它的行为仍然相同,这与谷歌图像搜索如何显示它的结果有关,在其他页面上也能正常工作。还在寻找
-
完美运行。您可能没有算到的是,图像是使用 Base64 字符串直接插入到 HTML 文档中的。没有
source。您必须获取字符串并对其进行解码。请参阅:Convert.FromBase64String。 -
它不适用于此网址:google.com/…
-
它有效。你解码图像了吗?
标签: vb.net winforms webbrowser-control