rvico TRichView Reference | TRichView

TCustomRichView.OnImportPicture

Top  Previous  Next

Occurs when loading a file or a stream containing links to image files.

type

  TRVImportPictureEvent =

    procedure (Sender: TCustomRichView;

    const Location: String; Width, Height: Integer;

    var Graphic: TGraphic) of object;

 

property OnImportPicture: TRVImportPictureEvent;

(introduced in version 1.8)

This event may occur:

when loading RTF. Usually pictures are saved inside RTF file/stream. But sometimes RTF contains links to external image files (file names or URLs).

when loading HTML (using RvHtmlImporter or RvHtmlViewImporter components)

when accepting URL to an image as a result of drag&drop, and rvoDragDropPicturesFromLinks is include in EditorOptions.

If these links are local (like 'c:\images\image.bmp'), TRichView can load these files itself. But if images are on the Internet (like 'http://www.trichview.com/images/headers/what_is.gif'), it makes sense to process this event and to download the image.

Input parameters

Location path to the file (local or URL);

Width, Height reserved, equal to 0;

Graphic nil.

Output parameters

Graphic set it to the loaded image. If you leave it equal to nil, TRichView will attempt to load the image itself.

(TRichView can load image file only if its extension is associated with some graphic class. Delphi does it automatically for TBitmap, TIcon, TMetafile, TJpegImage. If you use third-party graphic classes, you can register them using TPicture.RegisterFileFormat class procedure).

 

If you use RichViewActions, they can use downloader components to download pictures automatically, when loading is initiated by the action (Open, Insert File, Paste and Paste Special actions). This downloader can also be used in OLE drag&drop, see OnBeforeOleDrop and OnAfterOleDrop events.

 

Example: downloading images from http location using Indy (IdHTTP1: TIdHTTP)

Unfortunately, Delphi does not have methods returning graphic class by file extension. So we need to create a temporal file, save image there, then load it in TPicture.

uses RVFuncs;

 

{ Returns the system directory for temporal files }

function GetTempDir: String;

var l: Integer;

begin

  // Warning: : The GetTempPath function

  // does not verify that the returned

  // directory exists.

  SetLength(Result, 300);

  l := GetTempPath(300, PChar(Result));

  SetLength(Result, l);

end;

 

{ Returns name for temporal file with the

  given extension }

function GetTempFileName(const Ext: String): String;

var Path: String;

    v: Integer;

begin

  Path := GetTempDir+'tmp';

  v := Random(MaxInt div 2);

  repeat

    inc(v);

    Result := Path+IntToStr(v)+Ext;

  until not FileExists(Result);

end;

 

{ TMyRichView.OnImportPicture }

procedure TMyForm.MyRichViewImportPicture(Sender: TCustomRichView;

  const Location: String; Width, Height: Integer;

  var Graphic: TGraphic);

var Stream: TMemoryStream;

    FileStream: TFileStream;

    FileName: String;

begin

  if Pos('http://', LowerCase(Location))=1 then

  begin

    Stream := TMemoryStream.Create;

    try

      // 1. downloading

      IdHTTP1.Get(Location, Stream);

      // 2. creating temporal file

      FileName := GetTempFileName(ExtractFileExt(Location));

      FileStream := TFileStream.Create(FileName, fmCreate);

      try

        FileStream.CopyFrom(Stream, 0);

      finally

        FileStream.Free;

      end;

      // 3. reading and deleting temporal file

       Graphic := RVGraphicHandler.LoadFromFile(FileName);

       DeleteFile(FileName);

      end;

    except

      Graphic.Free;

      Graphic := nil;

    end;

    Stream.Free;

  end;

end;

 

See also methods of TRichView:

LoadRTF;

LoadRTFFromStream.

See also methods of TRichViewEdit:

InsertRTFFromFileEd;

InsertRTFFromStreamEd.


TRichView © trichview.com