简体   繁体   English

如何从 TWebBrowser 获取图像到 TImage

[英]how to get image from TWebBrowser into a TImage

I have a panel at the top of a form with a smallish TMemo set up as a drop target for URLs.我在表单顶部有一个面板,其中设置了一个小的 TMemo 作为 URL 的放置目标。 Below that I have a Page Control with 2 tabs.在此之下,我有一个带有 2 个选项卡的页面控件。 I set up a TWebBrowser in one tab and some other stuff in the second tab.我在一个选项卡中设置了 TWebBrowser,在第二个选项卡中设置了一些其他内容。 In the TWebBrowser, I need to login to a bug ticketing site we have, then browse tickets.在 TWebBrowser 中,我需要登录到我们拥有的 bug 票务网站,然后浏览票证。 Certain tickets have links to small images that clients want us to use (think something like logos).某些门票包含指向客户希望我们使用的小图像的链接(想想徽标之类的东西)。 I can left-click on them and then drag-n-drop the link from the TWebBrowser to the TMemo at the top of the form.我可以左键单击它们,然后将链接从 TWebBrowser 拖放到表单顶部的 TMemo。 That picks up the image's URL and Title as expected.这会按预期拾取图像的 URL 和标题。

The associated OnDrop event also switches to the 2nd tab and where I click a "Process" button.关联的 OnDrop 事件也切换到第二个选项卡,我在其中单击“处理”按钮。 That causes the browser to navigate to the image (via Navigate2), which it loads into the browser window.这会导致浏览器导航到图像(通过 Navigate2),然后将其加载到浏览器 window 中。 That image is all that's in the browser window at this point.此时,该图像就是浏览器 window 中的全部内容。

So now I need the code to grab the image and load it into a TImage on the 2nd tabsheet.所以现在我需要代码来抓取图像并将其加载到第二个标签页上的 TImage 中。 FWIW, the original image is a PNG. FWIW,原始图像是PNG。

I'm able to get the height and width of the image from the target image, but I'm not getting the image itself to show up in the TImage most of the time.我能够从目标图像中获取图像的高度和宽度,但大多数时候我并没有让图像本身显示在 TImage 中。 Sometimes, but mostly not.有时,但大多不是。 I can load PNGs into these image objects just fine, so I know it displays PNGs properly.我可以很好地将 PNG 加载到这些图像对象中,所以我知道它可以正确显示 PNG。

(Maybe there's a way to do a download the image directly, but I wasn't able to get that to work. The TWebBrowser is used to login to an internal bug ticketing system where the images are being provided, and I can access that site only if it's on the same form and I've logged in. Otherwise the DL loads an HTML login page instead of the image. If there's a way to do that in the current framework, I'm open to suggestions.) (也许有一种方法可以直接下载图像,但我无法让它工作。TWebBrowser 用于登录到提供图像的内部错误票务系统,我可以访问该站点仅当它在同一个表单上并且我已经登录时。否则,DL 会加载 HTML 登录页面而不是图像。如果在当前框架中有办法做到这一点,我愿意接受建议。)

I'm finding the image file on the web page using IHTMLElement2.getelementsByTagName('img') and grabbing the first one (since I know that's all there is on the web page).我使用IHTMLElement2.getelementsByTagName('img')在 web 页面上找到图像文件并获取第一个(因为我知道 web 页面上的所有内容)。

  img := getFirstImage;
  Image1_frame.Height := img.height+2;
  Image1_frame.Width := img.width+2;
  rnd := img as IHTMLElementRender ;
  rnd.DrawToDC(Image1.Canvas.Handle);

Image1 is aligned to Client on a panel named Image1_frame. Image1 在名为 Image1_frame 的面板上与客户端对齐。 So I set the frame's H & W -- they get set ok.所以我设置了框架的 H & W——它们设置好了。

But the image is usually not visible.但图像通常不可见。 It's just white.它只是白色的。

I see that DrawToDC is deprecated, but I haven't found what to replace it with.我看到 DrawToDC 已被弃用,但我还没有找到替换它的内容。

You can use this approach to auto login (if needed): Automated Log In (webBrowser)您可以使用此方法自动登录(如果需要): 自动登录 (webBrowser)

Then you can use this to get the Image: Image from TWebBrowser to TPicture然后你可以使用它来获取 Image: Image from TWebBrowser to TPicture

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

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