1

I have a panel at the top of a form with a smallish TMemo set up as a drop target for URLs. Below that I have a Page Control with 2 tabs. I set up a TWebBrowser in one tab and some other stuff in the second tab. In the TWebBrowser, I need to login to a bug ticketing site we have, then browse tickets. 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. That picks up the image's URL and Title as expected.

The associated OnDrop event also switches to the 2nd tab and where I click a "Process" button. That causes the browser to navigate to the image (via Navigate2), which it loads into the browser window. That image is all that's in the browser window at this point.

So now I need the code to grab the image and load it into a TImage on the 2nd tabsheet. FWIW, the original image is a 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. Sometimes, but mostly not. I can load PNGs into these image objects just fine, so I know it displays PNGs properly.

(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.)

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).

  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. So I set the frame's H & W -- they get set ok.

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.

David
  • 101
  • 1
  • 10
  • "*I see that DrawToDC is deprecated, but I haven't found what to replace it with*" - see [How to render WebBrowser to device context?](https://stackoverflow.com/questions/10884088/) – Remy Lebeau Jun 23 '20 at 01:37
  • "Maybe there's a way to do a download the image directly, but I wasn't able to get that to work.". yes, you can use a http client component to download any file provided you have the URL. You can use ICS (http://wiki.overbyte.be or use Delphi own GetIT to get it) or Indy. Some URL are part of a protected web area so you must also to the authentication using the HTTP component and/or pass thru some page to grab a cookie before the document will be delivered. – fpiette Jun 23 '20 at 13:00
  • I tried the DL approach with Indy, but kept getting back a big text block that was html that constituted a login page. I don't know what cookie is needed. As I said, that's why I put the browser on the first tab, so the other logic is operating within the same session as the initial login. How can I make a DL request once the user has logged-in on the first tab? That's what's unclear to me. I don't care if it's Indy or ICS, I'm just not clear how to do it given what I've found about it. – David Jun 23 '20 at 16:53
  • If I'm already logged-in via a TWebBrowser component, isn't there any way to issue a download request through it? Everything I'm seeing requires a separate component, or some really convoluted way to do this. I simply want the equivalent of right-click --> Save As ... I don't want to have to deal with a login page every time we download a file. – David Jun 24 '20 at 22:21

1 Answers1

1

You can use this approach to auto login (if needed) : Automated Log In (webBrowser)

Then you can use this to get the Image: Image from TWebBrowser to TPicture

L Q
  • 81
  • 4
  • The first part looks like it very well may work; I'll have to give it a try. But everybody keeps pointing me to the same examples that use the deprecated DrawToDC API that does NOT work. I'd like to see what to use in its place that's not 100+ lines of convoluted code. Usually when a function is deprecated, it's replaced with another function that does the same thing with different parameters or a slightly different approach. All I've seen is dozens of lines of code that use obscure parts of the API that have virtually no documentation I can find. Doesn't DrawToDC have a simple replacement? – David Aug 08 '20 at 06:32