Examples

<< Click to Display Table of Contents >>

Navigation:  Programming > Objects > Images > C) Work with images >

Examples

1) InsertGraphicDialog

 

The easiest way to insert an image is to call the method InsertGraphicDialog. It has the optional parameters  

  filter: string = '';

  InsertLink: Boolean = FALSE;

  ObjectModes: TWPTextObjModes = [];

  path : string = ''

 

filter can be used to specify a file extension filter for the open dialog.

InsertLink = true will create a linked image. When a file is saved, such an image will not be embedded. Instead the filename will be saved and, at load time, provided to the event OnRequestHTTPImage.  

A linked image has a non empty property StreamName.

ObjectModes contains a set of flags which will be used for the new image object. You can specify wpobjRelativeToParagraph or wpobjRelativeToPage to insert a movable image.

path is the optional initial director for the open dialog.

 

Example:

procedure TForm1.BtnInsertImageDialogClick(Sender: TObject);

begin

 WPRichText1.InsertGraphicDialog(

    'Image Files@*.bmp',

    false,

    [wpobjRelativeToParagraph],

    ExtractFilePath(Application.EXEName));

end;

 

2) Insert a linked image, Alternative 1

 

We simply provide a name, i.e. "RED" which is used in the event OnRequestHTTPImage to load an image. This can be very useful in database applications to store the images outside of the text blobs to improve performance.

 

procedure TForm1.InsertLinkedImageClick(Sender: TObject);

begin

  WPRichText1.InsertGraphic('RED', true, []);

end;

 

procedure TForm1.WPRichText1RequestHTTPImage(

 RTFData: TWPRTFDataCollectionBase; Reader: TWPCustomTextReader;

const LoadPath, url: String; TextObject: TWPTextObj; var Ok: Boolean);

begin

 TextObject.LoadObjFromFile(

    ExtractFilePath(Application.EXEName) + url + '.bmp');

 Ok := true;

end;

 

3) Insert a linked image - Alternative 2 for for delayed loading

 

Instead of loading the image in  It is also possible use the event OnTextObjectPaint for this. This has the advantage that the image can be loaded later and only if it is visible. It is also possible to load alternative, smaller versions of the same image.

 

procedure TForm1.InsertImageForOnPaintClick(Sender: TObject);

begin

  WPRichText1.TextObjects.InsertNewObject(wpobjImage,'GREEN');

  WPRichText1.ReformatAll(false, true);

end;

 

procedure TForm1.WPRichText1TextObjectPaint(Sender: TObject;

 pobj: TWPTextObj; toCanvas: TCanvas; XRes, YRes, X, Y, W, H,

 BASE: Integer; PageRef: TWPVirtPage; Modes: TWPTextObjectPaintModes;

const CanvasExtraAttr: TWPPaintExtraParams;

var ContinueMode: TWPTextObjectPaintResult);

var s : String;

begin

if (pobj.ObjType=wpobjImage) and (pobj.ObjRef=nil) then

begin

   s := ExtractFilePath(Application.EXEName) + pobj.Source  + '.bmp';

  if (s<>'') and FileExists(s) then

  begin

    pobj.LoadObjFromFile(s);

    pobj.ObjRef.StreamName := pobj.Source; // MAKES it "LINKED"

    pobj.GetWHFromContents(1);

  end;

   ContinueMode := ContinueMode - [wpobjPaintRedCross];

end;

end;

 

It is required to set the property ObjRef.StreamName to avoid embedding the image data when the text is saved.

 

4) Insert a graphic as object:

 

procedure TForm1.InsertGraphicClick(Sender: TObject);

var txtobj: TWPTextObj;

begin

if OpenDialog2.Execute and (WPRichText1.ActiveParagraph <> nil) then

begin

   WPRichText1.SetFocus;

   txtobj := WPRichText1.Memo.RTFData.TextObjects.Insert(

     WPLoadObjectFromFile(

         WPRichText1.Memo.RTFData,

         OpenDialog2.FileName), 1440, 1440);

  // Code to change the graphic, for example change width and height ot the 'PositionMode'

  if txtobj <> nil then

  begin

 

 

  end;

   WPRichText1.Refresh;

end;

end;

 

Note: If you need to work directly with a TParagraph you can use the method TParagraph.AppendNewObject to create a new TWPTextObj object. To the ObjRef property of this object you can assign the TWPObject instance created by WPLoadObjectFromFile. (See CreateTable demo)

 

 

4) Modify the currently selected object:

 

With TextObjects.SelectedObj You have access to the TWPTextObj which is the anchor of the current object.

 

procedure TForm1.ChangeObjectPositionAndWrapMode(Sender: TObject);

begin

  if WPRichText1.TextObjects.SelectedObj<>nil then

  begin

       WPRichText1.TextObjects.ChangePositionMode(

            WPRichText1.TextObjects.SelectedObj,

       wpotPar, wpwrBoth);

  end;

end;

Hint: It would be also possible to use modify WPRichText1.TextObjects.SelectedObj.Wrap and WPRichText1.TextObjects.SelectedObj.PositionMode directly but this change is not logged for undo.

 

 

5) Load new image into object:

 

procedure TForm1.Loadimage1Click(Sender: TObject);

begin

if (WPRichText1.TextObjects.SelectedObj<>nil) and

     (OpenPictureDialog1.Execute) then

    begin

        WPRichText1.TextObjects.SelectedObj.LoadObjFromFile(

           OpenPictureDialog1.FileName);

    end;

end;

 

 

6) Search and Replace text with graphic file

 

var GSFile: string;

   TextObject: TWPOImage; // from unit WPObj_Image

begin

 GSFile := 'C:\testfile.jpg';

if FileExists(GSFile) then

  with WPRichText1 do

  begin

     Finder.ToStart;

    while Finder.Next('<<Graphic-Signature>>') do

    begin

       TextObject := TWPOImage.Create(WPRichText1.Memo.RTFData); // !

       TextObject.LoadFromFile(GSFile);

       SetSelPosLen(Finder.FoundPosition, Finder.FoundLength);

       TextObjects.Insert(TextObject);

    end;

  end;

end;

 

Alternative, using a graphic from an TImage object. This works fro instant screen display, but if you intend to save the document later better load the image data into the object.

 

WPRichText1.Finder.ToStart;

while WPRichText1.Finder.Next('[sig]') do

begin

 WPRichText1.CPPosition := WPRichText1.Finder.FoundPosition;

 WPRichText1.Finder.FoundText := '';

 WPRichText1.TextObjects.InsertCopy(Image1.Picture.Graphic);

end;

WPRichText1.DelayedReformat;