Page 1 of 1

Background Image

Posted: Wed Feb 24, 2010 11:21 am
by stuart
Silly question, I'm sure. When I make the following call, The image drops nicely into the editor.

var
img: TImage;
begin
if not dlgOpenPicture.Execute then
Exit;

img := TImage.Create(nil);
img.Picture.LoadFromFile(dlgOpenPicture.FileName);
rvEdit.InsertPicture('', img.Picture.Graphic, rvvaBaseLine);
end;

However, if I call rvEdit.BackgroundBitmap := img.Picture.Bitmap; instead of InsertPicture, the background just stays white.

Can someone tell me how I can add a background image please?

Many thanks in advance.

Stuart

Posted: Wed Feb 24, 2010 6:48 pm
by Sergey Tkachenko
1. Your code has a memory leak. TRichView will free img.Picture.Graphic, but img itself will not be freed.
Use this code:

Code: Select all

var 
  img: TImage; 
  gr: TGraphic;
begin 
  if not dlgOpenPicture.Execute then 
    Exit; 
  img := TImage.Create(nil); 
  try
    img.Picture.LoadFromFile(dlgOpenPicture.FileName); 
    gr :=   RV_CreateGraphics(TGraphicClass(img.Picture.Graphic.ClassType)); // defined in RVFuncs.pas
    gr.Assign(img.Picture.Graphic)
    rvEdit.InsertPicture('', gr, rvvaBaseLine); 
  finally
    img.Free;
  end;
end; 

Posted: Wed Feb 24, 2010 6:53 pm
by Sergey Tkachenko
2. Assign BackgroundStyle property. By default it is bsNoBitmap, meaning no picture. Other property values specifies position, stretching and tiling for the background image.

Code: Select all

var 
  img: TImage; 
begin 
  if not dlgOpenPicture.Execute then 
    Exit; 
  img := TImage.Create(nil); 
  try 
    img.Picture.LoadFromFile(dlgOpenPicture.FileName); 
    rvEdit.BackgroundBitmap := img.Picture.Bitmap; // convert picture to bitmap and copy it
    rvEdit.BackgroundStyle := bcCentered;
  finally 
    img.Free; 
  end; 
end; 
PS: You can create and use TPicture object instead of TImage.

Posted: Thu Feb 25, 2010 11:44 am
by stuart
Sergey

Thanks for the assistance, and for pointing out my memory leak.

I have tried your code, and at first it didn't seem to work, but then I tried with a bitmap, and it worked. is there anything I need to do if using non Jpegs??

Thanks

Stuart