Background Image

General TRichView support forum. Please post your questions here
Post Reply
stuart
Posts: 8
Joined: Wed Jan 13, 2010 4:48 pm

Background Image

Post 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
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post 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; 
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post 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.
stuart
Posts: 8
Joined: Wed Jan 13, 2010 4:48 pm

Post 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
Post Reply