List Index Out Of Bounds When Inserting Bitmap

General TRichView support forum. Please post your questions here
Post Reply
mandrillboy
Posts: 6
Joined: Thu Jul 16, 2009 9:29 am

List Index Out Of Bounds When Inserting Bitmap

Post by mandrillboy »

I have been using the following code to replace the selection with a bitmap image:

procedure MergeFieldToBitmap(var Bmp: TBitmap; const ImgName: string; const StretchX,StretchY: Integer);
var
info: TRVGraphicItemInfo;
s, SplitText: TRVRawByteString;
begin
re.DeleteSelection;
info := TRVGraphicItemInfo.CreateEx(RVData, Bmp, rvvaBaseline);
info.ImageWidth := Bmp.Width * StretchX;
info.ImageHeight := Bmp.Height * StretchY;
s := RVU_GetRawUnicode(ImgName);
SplitText := '';
TRVEditRVData(re.RVData).InsertSomething(info, s, SplitText, False, False, True);
Bmp := nil;
end; {MergeFieldToBitmap}


Having upgraded to the latest version this code now causes a list index out of bounds error on the InsertSomething command.

The error is raised in unit RVERVData on line 1919

As follows:

then begin
info.SameAsPrev := True;
info.ParaNo := GetItemPara(DrawItems[CaretDrawItemNo].ItemNo); //< HERE
end
else begin


CaretDrawItemNo = -1

Crucially the code is called before the form is displayed. Is there a fix/workaround for this?
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Why cannot you just use InsertPicture method, instead of undocumented methods?
mandrillboy
Posts: 6
Joined: Thu Jul 16, 2009 9:29 am

Post by mandrillboy »

I need to manually set the width and height.

However InsertPicture raises the exact same error.
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

The recommended way for inserting resized picture is:

Code: Select all

re.BeginUpdate;
re.TopLevelEditor.BeginUndoGroup(rvutInsert);
re.TopLevelEditor.SetUndoGroupMode(True);
try
  if re.InsertPicture(...) then begin
    re.SetCurrentItemExtraIntProperty(rvepImageWidth, StretchX, True);
    re.SetCurrentItemExtraIntProperty(rvepImageHeight, StretchY, True);
  end;
finally
  re.TopLevelEditor.SetUndoGroupMode(False);
  re.EndUpdate;
end;
It is guaranteed that it will work in future versions, unlike using undocumented methods.

As for the error, the most possible reason for it is unformatted document at the moment of the image insertion.
mandrillboy
Posts: 6
Joined: Thu Jul 16, 2009 9:29 am

Post by mandrillboy »

Thanks!

Calling 'Format' after loading the document fixed the error (I was calling Reformat before)

Almost there, this code does not place the image in a table cell however.

I am sure I am missing something really simple here so will check the documentation.
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Format moves the caret position to the beginning of the document.
Reformat requires that the document was formatted before calling Reformat, and changes after the last formatting did not change document structure (for example, no items were added/deleted)
mandrillboy
Posts: 6
Joined: Thu Jul 16, 2009 9:29 am

Post by mandrillboy »

All sorted,

just to confirm for the benefit of other, the main cause of the issue was failing to call Format after loading the document.
Post Reply