How to change picture delphi timage in run time

8.2k Views Asked by At

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;
2

There are 2 best solutions below

19
On

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.
0
On

I had the same problem today. After the call of LoadFromFile() the image does not change. I have tried Refresh, Repaint, Invalidate and Update -> nothing helped. Then I found that resizing the from immediately updated the image. Finally I found that setting property Visible to false and back to true updates the image, too.

FormMain.Image1.Visible := false;
FormMain.Image1.Picture.LoadFromFile(newImageFileName);
FormMain.Image1.Visible := true;

Perhaps not the best but it works for me.