Recent

Author Topic: AccessViolation using TPicture.LoadFromFile  (Read 956 times)

meteoricshipyards

  • New member
  • *
  • Posts: 8
AccessViolation using TPicture.LoadFromFile
« on: October 17, 2023, 09:43:05 am »
Just trying to load a TPicture to put into a Timage on the screen, and getting an External: Access Violation.  I don't know why.  I put the picture (tried JPG and PNG formats - didn't make a difference).  If I put in a non-existent file name, it gives a different error.  Checked the properties of the file, seems to alright, and I can access the file with other programs, so I don't think it's a real problem.  The code it points to, picture.inc line 688 is the only line in this routine:
Code: Pascal  [Select][+][-]
  1. procedure TPicture.LoadFromStreamWithFileExt(Stream: TStream;
  2.   const FileExt: string);
  3. begin
  4.   LoadFromStreamWithClass(Stream, FindGraphicClassWithFileExt(FileExt));
  5. end;    

My unit looks like this:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Image1: TImage;
  16.     procedure init(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.   MyImg:  TPicture;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.init(Sender: TObject);
  34.    var
  35.       CurrentDir: String;
  36.       Filename: String;
  37.    begin
  38.    //ProgramDir := ExtractFileDir(ParamStr(0));
  39.    //ShowMessage(ProgramDir);
  40.    CurrentDir := GetCurrentDir;
  41.    ShowMessage(CurrentDir);
  42.    Filename := CurrentDir + '\Blank.jpg';
  43.    MyImg.Create;
  44.    MyImg.LoadFromFile(Filename);
  45.    Image1.Picture:= MyImg;
  46.    Image1.Repaint;
  47. end;
  48.  
  49. end.

Thanks for any help!

Tom A.

"Since the only way to reach the Sky Mountain was by air, the native prohibition against airships made no sense." - The InUncredible Adventures of Baron Karnath

Handoko

  • Hero Member
  • *****
  • Posts: 5526
  • My goal: build my own game engine using Lazarus
Re: AccessViolation using TPicture.LoadFromFile
« Reply #1 on: October 17, 2023, 09:49:16 am »
This is wrong:

Code: Pascal  [Select][+][-]
  1.   MyImg.Create;

It should be:

Code: Pascal  [Select][+][-]
  1.   MyImg := TPicture.Create;

meteoricshipyards

  • New member
  • *
  • Posts: 8
Re: AccessViolation using TPicture.LoadFromFile
« Reply #2 on: October 17, 2023, 11:01:46 am »
Thank you very much.  Fixed it!

Tom A.

 

TinyPortal © 2005-2018