Anewil
Anewil

Reputation: 119

Lazarus - loading picture form file

I have an issue with loading a picture from z file. The picture has a .png extension. Can anyone explain me why it doesn't work anymore ?

 if (FileExists('file.png')) then
   Image1.Picture.LoadFromFile('file.png');  

Errors:

Project project1 raised exception class 'PNGImageException' with message: This is not PNG-data

Project project1 raised exception class 'FPImageException' with message: Wrong image format

Upvotes: 2

Views: 5394

Answers (1)

A. Kasprzycki
A. Kasprzycki

Reputation: 21

At the first create your image component:

MyPicture := TImage.Create(FormCanvas);
MyPicture.Name := 'picture';
MyPicture.Parent := FormCanvas;

V1 - use direct path

Edit_pic_path.Text := 'C:\Images\';
Prop_Picture_Name.Text := 'image.png';

try
     if (FileExists(Edit_pic_path.Text + Prop_Picture_Name.Text)) then
     begin
       MyPicture.Picture.LoadFromFile(Edit_pic_path.Text + Prop_Picture_Name.Text);
     end;
  finally
  end;

V2 - image must be in project folder

...
MyPicture.Picture.LoadFromFile(Prop_Picture_Name.Text);
...

Upvotes: 1

Related Questions