|
TCustomRichView.OnImportPicture |
Top Previous Next |
|
Occurs when loading RTF file or stream containing links to image files. This event also can be used by TRVHtmlImporter. type TRVImportPictureEvent = procedure (Sender: TCustomRichView; const Location: String; Width, Height: Integer; var Graphic: TGraphic) of object;
property OnImportPicture: TRVImportPictureEvent; (introduced in version 1.8) Usually pictures are saved inside RTF file/stream. But sometimes RTF contains links to external image files (file names or URLs). 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).
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; pic: TPicture; 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 pic := TPicture.Create; try pic.LoadFromFile(FileName); Graphic := RV_CreateGraphics(TGraphicClass(pic.Graphic.ClassType)); Graphic.Assign(pic.Graphic); finally pic.Free; DeleteFile(FileName); end; except Graphic.Free; Graphic := nil; end; Stream.Free; end; end;
See also methods of TRichView:
See also methods of TRichViewEdit: |