1

How can I make my program load an image and make it the background for a form? I need the exact code for it. I've looked all over the internet and the only things I've found are various tweaks and fixes to make backgrounds work as intended in special circumstances. I've also tried some Delphi books I have and I can't find it anywhere.

Aleksa
  • 21
  • 1
  • 1
  • 3
  • dup question http://stackoverflow.com/questions/525970/how-to-add-background-images-to-delphi-forms – BlackTigerX Dec 04 '09 at 22:03
  • No, Tiger, it's not a duplicate. Look at the other question and you'll see that the person asking it *already had* a background image and was really asking how to resolve some cosmetic issues, and that's what the answers to that question addressed. – Rob Kennedy Dec 04 '09 at 22:14

4 Answers4

4
  1. Put a TImageon your form. Make sure it's behind all other controls on the form. You can right-click it and choose the "send to back" menu option.

  2. Load a graphic.

    var
      img: TBitmap;
    begin
      img := TBitmap.Create;
      try
        img.LoadFromFile('S:\background.bmp');
    
  3. Assign it to the image control.

        Image1.Picture := img;
    
  4. Clean up.

      finally
        img.Free;
      end;
    end;
    

You can also combine the last three steps to load the graphic and put it in the image control all at once. Thanks to Jon for the suggestion.

Image1.Picture.LoadFromFile('B:\background.bmp');

See also: How to add background images to Delphi forms

Community
  • 1
  • 1
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • I would love some feedback explaining what wasn't useful about this answer. Were there factual errors that I can correct, or is it fundamentally an unworkable solution? – Rob Kennedy Dec 04 '09 at 22:19
  • 1
    Why not load the image directly by the TImage control? That way you'd be able to use any image format that was loaded into TPicture (jpg, gif, etc..) – Jon Benedicto Dec 04 '09 at 22:31
  • Because I can't remember whether the Picture property is nil at first, and I can't remember whether TPicture knows how to load images by itself. Neither are problems, apparently. Thanks for the ideas. – Rob Kennedy Dec 04 '09 at 22:37
0

This is the way all my applications show a form image. I load the image at form creation or when the application calls a specific showing event

  var
    vDest, vRect:  TRect;
  begin
    vRect := Rect(0, 0, FBackgroundImage.Width, FBackgroundImage.Height);
    vDest := Rect(0,0,Self.Width, Self.Height);
    Canvas.StretchDraw(vDest, FBackgroundImage);


  if FileExists(this) then
    FBackgroundImage.LoadFromFile(this);
McDowell
  • 107,573
  • 31
  • 204
  • 267
Brendan
  • 55
  • 1
  • 7
0

@Brendan

thanks
//from Brendan code;

var
vDest, vRect:  TRect;
FBackgroundImage: TGraphic;
begin
FBackgroundImage := image1.Picture.Graphic; //LOAD from invisible image
vRect := Rect(0, 0, FBackgroundImage.Width, FBackgroundImage.Height);
vDest := Rect(0,0,Self.Width, Self.Height);
Canvas.StretchDraw(vDest, FBackgroundImage);
end;
XenKid
  • 161
  • 3
  • 16
0

What I would do is use the forms OnPaint event, get the canvas (Form1.Canvas), and then use the Draw method (which takes an image) to draw the image you want. Something like the following:

procedure TForm1.FormPaint(Sender: TObject);
var
mypic: TBitMap;
begin
mypic := TBitMap.Create;
try
mypic.LoadFromFile('cant.bmp');
Form1.Canvas.Draw(0, 0, mypic);
finally
FreeAndNil(mypic);
end;
end;

Note that this could be extremely slow.

Tom A
  • 1,662
  • 2
  • 23
  • 41
  • 1
    You mention it as a note, but it really isn't a good idea to load a file in the paint event. Also, you should probably call the inherited `Paint` implementation. – jpfollenius Dec 05 '09 at 15:30
  • true. what would be better would be to load the image on the oncreate or onshow, then save it for re-use so it's not always re-loading when repainting. Also, frequent updates to the application will slow this down as well. – Tom A Dec 05 '09 at 19:37