Wednesday, April 08, 2009

Delphi: Using TWebBrowser to display content from stream rather than URL

TWebBrowser is a wrapper for Internet Explorer COM interface.  The most common way to use TWebBrowser is using Navigate method that accept a URL string.  However, this is not convenient for user who already has a rendered HTML string or stream.  One workaround solution for this is save the string or stream to a temporary file and use supply a local file URL to TWebBrowser.Navigate.

Here is a solution for supplying a HTML stream to TWebBrowser:

uses ActiveX;

const IID_IPersistStreamInit : TGUID = '{7FD52380-4E07-101B-AE2D-08002B2EC713}';

procedure TForm19.Button1Click(Sender: TObject);
var S: TStream;
    o: IPersistStreamInit;
  S := TStringStream.Create('<html><body>this is a test</body></html>');
    while WebBrowser1.ReadyState <> Shdocvw.READYSTATE_COMPLETE do

    if Supports(WebBrowser1.Document, IID_IPersistStreamInit, o) then


  1. Component to display HTML read from a stream rather than fetched from a URL. Author: Antonio Estevez. URL:


Jelmer said...

Great, but what if you want to stream more than only html? Something like this:

S := TStringStream.Create('[html][body]this is a image: [img src="myimg:picture.jpg"][/body][/htlm]');

The file picture.jpg is not on disk, but in memory (TMemoryStream).

LDS said...

A better wrapper around the web browser control can be found here: - some rough edges but fra more powerful control - that's the kind of work Borland/Codegear should have done in the past 8/10 years instead of chasing any new buzzword.
@Jelmer: about loading images the way you like, you should implement an Asynchronous Pluggable Protocols - MSDN has the details (it requires to implement some COM interfaces), I did it once to display images embedded into HTML mail messages.

Erik said...

Forget relying on Microsoft's junk and their nutso COM+ interfaces. Use PBear's HTML Display components, available here:

As of last year, these components are free. (We've been a paying customer for something like 10 years now, and the controls work great.)

LDS said...

PBear controls support only HTML 3.2, 4.0 partially and no Javascript - good if the developer can control what is fed to the control and has no high-end needs. They are no longer supported also.