Insert Image which is loaded from the WEB

    • Offizieller Beitrag

    If you need to insert an image in the document which data is loaded from the web you have to use a component to access the internet. In our example we use Indy.

    Please drop an INDY TIdHTTP component on the form:

    Code
    object IdHTTP1: TIdHTTP  MaxLineAction = maException  ReadTimeout = 0  AllowCookies = True  ProxyParams.BasicAuthentication = False  ProxyParams.ProxyPort = 0  Request.ContentLength = -1  Request.ContentRangeEnd = 0  Request.ContentRangeStart = 0  Request.ContentType = 'text/html'  Request.Accept = 'text/html, */*'  Request.BasicAuthentication = False  Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'  HTTPOptions = [hoForceEncodeParams]  Left = 554  Top = 35end

    This code can be used to create the image:

    Code
    procedure TForm1.InsertWEBPictureClick(Sender: TObject);var loadurl : String;    obj : TWPTextObj;    stream : TMemoryStream;begin  loadurl := 'http://www.wpcubed.com/images/wpsynscreen_400.gif';  stream := TMemoryStream.Create;  try    IdHTTP1.Get(loadurl,stream);    if stream.Size>0 then    try       obj := WPRichText1.TextObjects.InsertNewObject(wpobjImage,false, false);       obj.LoadObjFromStream(loadurl,stream);       obj.ObjRef.StreamName := loadurl;    except       obj.Free;    end;  finally    stream.Free;  end; end;

    You will need to add 2 events to update the object data after the file was saved and reloaded. This are the events OnRequestHTTPImage and (this is important to load CSS styles) OnRequestHTTPString.
    The following events modify the caption of a panel to show which file is currently downloaded.

    If you load a file which was placed on a website and which is using relative links it is necessary to make the path to the website known by the reader:

    WPRichText1.Memo.FileLoadPath := url_of_html_text;
    WPRichText1.AsString := html_text;

    note: you can use
    html_text:= IdHTTP1.Get(url_of_html_text);

    to retrieve the text from a web server.