0
votes

I use a timage in a form which load a background image. The problem is when i choose another picture in run time and change it by Img_Bk.Picture.LoadFromFile( SaveFileName ); It doesnt work (Picture did n't change ). I mean it shows previous picture and doesn't show the new image during run time. Id like to change application background image during run time in my company by users which main form is a mdi form . I use delphi 7 .

    try
        Img_Bk.Picture := nil ;
        if FileSize > 100 then
        begin
            Img_Bk.Picture.LoadFromFile( SaveFileName );
        end;
        Img_Bk.Stretch := True ;
    except
    end;
1
Please add more details to your question (real source, real error messages, ...) - Sir Rufo
Please do better than "It doesn't work". In what way? Never say "it doesn't work". - David Heffernan
David sorry , I didnt get your mean . Id like to change my background image in runtime . - Abl

1 Answers

2
votes

LoadFromFile is known to work. So there must be a more prosaic explanation.

The first possible explanation is that FileSize is not greater than 100 and the if condition evaluates false.

Another possible explanation is that the image in the file that you specify is not the one you are expecting.

Otherwise, your code has a swallow all exception handler. And so when the call to LoadFromFile fails and raises an exception, your code ignores that and carries on as if nothing un-toward had happened. Remove the try/except, and deal with the error that will be revealed.

The real lesson for you to learn is never to write such an exception handler again.


This program should prove to you that LoadFromFile is just fine:

program ImageDemo;

uses
  Types, Math, IOUtils, SHFolder, Forms, Controls, StdCtrls, ExtCtrls, jpeg;

var
  Form: TForm;
  Image: TImage;
  Timer: TTimer;
  ImageIndex: Integer = -1;
  MyPictures: string;
  Images: TStringDynArray;

type
  THelper = class
    class procedure Timer(Sender: TObject);
  end;

class procedure THelper.Timer(Sender: TObject);
begin
  inc(ImageIndex);
  if ImageIndex>high(Images) then
    ImageIndex := 0;
  if ImageIndex>high(Images) then
    exit;
  Image.Picture.LoadFromFile(Images[ImageIndex]);
end;

function GetMyPictures: string;
var
  Str: array[0..260] of Char;
begin
  if SHGetFolderPath(0, CSIDL_MYPICTURES, 0, 0, Str) = S_OK then
    Result := Str;
end;

procedure BuildForm;
begin
  Form.ClientWidth := 700;
  Form.ClientHeight := 500;
  Image := TImage.Create(Form);
  Image.Parent := Form;
  Image.Align := alClient;
  Image.Stretch := True;
  Timer := TTimer.Create(Form);
  Timer.OnTimer := THelper.Timer;
  Timer.Interval := 100;
end;

begin
  MyPictures := GetMyPictures;
  Images := TDirectory.GetFiles(MyPictures, '*.jpg', TSearchOption.soAllDirectories);
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm, Form);
  BuildForm;
  Application.Run;
end.