Recent

Author Topic: TPicture.LoadFromFile not working  (Read 14377 times)

lainz

  • Guest
TPicture.LoadFromFile not working
« on: September 12, 2010, 12:14:29 am »
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1MouseEnter(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  picture1: TPicture;
  bitmap1: TBitmap;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Image1MouseEnter(Sender: TObject);
begin
  Image1.Picture:=picture1;
end;

initialization

picture1.Create;
picture1.LoadFromFile('1.bmp');

end.

Error: External SIGSEGV; and load "picture.inc" at line 730 " NewGraphic.OnProgress := @Progress;"

also the same error on bitmap.LoadFromFile

What i'm doing wrong?

jhvhs

  • New Member
  • *
  • Posts: 18
Re: TPicture.LoadFromFile not working
« Reply #1 on: September 12, 2010, 09:35:52 am »
This should do the trick:
Code: [Select]
procedure TForm1.Image1MouseEnter(Sender: TObject);
begin
  Image1.Picture.Assign(picture1);
end;
...
Picture1 := TPicture.Create;
« Last Edit: September 15, 2010, 10:06:40 pm by jhvhs »

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1891
Re: TPicture.LoadFromFile not working
« Reply #2 on: September 12, 2010, 11:23:25 am »
It doesn't have to do with Picture but with the way you're creating objects.

picture1.Create;
Should be:
picture1:=TPicture.create;

lainz

  • Guest
Re: TPicture.LoadFromFile not working
« Reply #3 on: September 12, 2010, 04:27:06 pm »
Thanks! Now i have an image button with 3 pictures (normal, mouse over, mouse down)

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image1MouseEnter(Sender: TObject);
    procedure Image1MouseLeave(Sender: TObject);
    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  picture1: TPicture;
  picture2: TPicture;
  picture3: TPicture;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Image1MouseEnter(Sender: TObject);
begin
  Image1.Picture.Assign(picture2);
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Image1.Picture.Assign(picture3);
end;

procedure TForm1.Image1MouseLeave(Sender: TObject);
begin
  Image1.Picture.Assign(picture1);
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Image1.Picture.Assign(picture2);
end;

initialization

{normal}
picture1:=TPicture.Create;
picture1.LoadFromFile('1.png');

{mouse over}
picture2:=TPicture.Create;
picture2.LoadFromFile('2.png');

{mouse down}
picture3:=TPicture.Create;
picture3.LoadFromFile('3.png');

end.

wotanica

  • Guest
Re: TPicture.LoadFromFile not working
« Reply #4 on: September 21, 2010, 03:36:04 pm »
Not to sound harsh but you should be carefull where you create objects. I for one would not setup picture-objects during unit initialization (unless they are global and have proper access methods). You are bound to get access violation errors sooner or later. If the pictures are part of a form, or being used by a component, you should keep them within the scope of that parent rather than on unit level:

Code: [Select]
(* override constructor *)
Constructor TForm1.Create(AOwner:TComponent);
Begin
  Inherited Create(AOwner);
  FPicture1:=TPicture.Create(NIL);
  (.. etc ..)
end;

Destructor TForm1.Destroy;
Begin
  if assigned(FPicture1) then
  FreeAndNIL(FPicture1);
  Inherited;
end;

The reason this is better is twofold:
1. You dont have to look for bizare errors outside the class (if something goes wrong with "picture1" the exception might turn up inside a form method, but it's actually on unit level. Better to keep these things seperated).
2. You have little control over how lazarus unloads units. If you initialize pictures this way - you at least have to take height of application.termination.

Just a friendly heads up, you are going to experience problems if you go down that road of programming.
« Last Edit: September 21, 2010, 03:45:04 pm by wotanica »

 

TinyPortal © 2005-2018