0

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;
Abl
  • 63
  • 2
  • 10

2 Answers2

2

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.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks David , the problem is not Filesize or try/except, i run the program step by step and all line are executed without any problem , but image doesnt change . – Abl Jan 26 '14 at 10:00
  • In that case the image in the file that you specify is the wrong one. We need to start from the premise that LoadFromFile works. – David Heffernan Jan 26 '14 at 10:02
  • Are you still stuck on this. Surely there are no possibilities other than the three that I list here. – David Heffernan Jan 26 '14 at 11:49
  • David unfortunately i stuck !:( ,I removed Img_Bk.Picture := nil ; and it didnt work . – Abl Jan 26 '14 at 12:29
  • Again you say "it didn't work". Sigh. You removed the try/except already right? – David Heffernan Jan 26 '14 at 12:46
  • David , Sorry . I remove everything and only Img_Bk.Picture.LoadFromFile( SaveFileName ); left . In run time when i choose new picture , background image doesn't change ! David id like to thanks to you deeply . – Abl Jan 26 '14 at 12:56
  • Works fine here. What can we do if we cannot reproduce? Now, if you made an SSCCE then it would be easy to be more specific. – David Heffernan Jan 26 '14 at 13:03
  • Hi David ,you are right . I understood cause of using Mdiform , the timage cann`t change the picture during run time . if I use the normal form it work correctly . – Abl Jan 27 '14 at 06:04
  • David it does`n work for Mdi form , believe that . I changed form style to fsnormal and the Timage picture's changed .Then i changed into fsMdiform (the only one change in form without any other change !) it didn't change ! :( – Abl Jan 27 '14 at 11:01
  • You want to paint on the MDI form? See my second answer here: http://stackoverflow.com/questions/15132326/tile-center-image-in-the-forms-background – David Heffernan Jan 27 '14 at 11:04
  • No , Id Like to change application background image during run time in my company by users which main form is a mdi form . – Abl Jan 27 '14 at 11:47
  • What do you mean by "application background"? Can you put an image on an image sharing site? – David Heffernan Jan 27 '14 at 11:48
  • Also, I'm sure I successfully answered the question that you asked. – David Heffernan Jan 27 '14 at 11:49
  • Thank you David for your pay attention. Unfortunately my problem did not solve. I know you try to help me . thanks alot – Abl Jan 27 '14 at 12:16
  • I'm not claiming to have solved your problem. I'm claiming to have answered your question. They two are quite different. Not least because your actual problem appears to be related to MDI which is not in the question that you asked. Take a look again at the question that you asked. – David Heffernan Jan 27 '14 at 12:18
  • Yes i know , because at the first i didnt know the problem is related to form style – Abl Jan 27 '14 at 12:25
  • That's on you. I answered the question that you asked. Anyway, you did not answer my questions. What do you mean by "application background"? Can you put an image on an image sharing site? – David Heffernan Jan 27 '14 at 12:29
  • I'd like to help you but you are making it very hard. Your latest edit is galling. – David Heffernan Jan 27 '14 at 12:33
  • That looks awfully like you want the container of the MDI children to have that background image. In which case the link I gave you an hour ago is what you need. You cannot do it properly with a `TImage` control. – David Heffernan Jan 27 '14 at 12:55
1

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.

Jo. D.
  • 21
  • 3