Page 1 of 1

List Index Out Of Bounds When Inserting Bitmap

Posted: Wed May 02, 2012 10:51 am
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?

Posted: Wed May 02, 2012 8:06 pm
by Sergey Tkachenko
Why cannot you just use InsertPicture method, instead of undocumented methods?

Posted: Thu May 03, 2012 12:49 pm
by mandrillboy
I need to manually set the width and height.

However InsertPicture raises the exact same error.

Posted: Thu May 03, 2012 1:23 pm
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.

Posted: Fri May 04, 2012 10:45 am
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.

Posted: Fri May 04, 2012 11:32 am
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)

Posted: Fri May 04, 2012 1:15 pm
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.