Recent

Author Topic: Using PNG images inside resource files in Lazarus  (Read 24998 times)

JR1234

  • New Member
  • *
  • Posts: 11
Using PNG images inside resource files in Lazarus
« on: April 17, 2014, 05:28:05 am »
Hello,

I am having problems using PNG files inside resources.

Here is the simplest example.  A form with a single image on it.

images.rc contains one line;
Code: [Select]
logo RCDATA "logo.png"
I add the images rc after the usual $R
Code: [Select]
{$R *.lfm}
{$R images.rc}

form.create is
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
var png:TPortableNetworkGraphic;
begin
     png:=TPortableNetworkGraphic.Create;
     try
        Png.LoadFromLazarusResource('logo');
        Image1.Picture.Graphic := Png;
     finally
        Png.Free;
     end;
end;

But when I try and run it I get the error

Project project1 raised exception class 'EResNotFound' with message
Resource 'logo' not found.

Any ideas?

totya

  • Hero Member
  • *****
  • Posts: 720

JR1234

  • New Member
  • *
  • Posts: 11
Re: Using PNG images inside resource files in Lazarus
« Reply #2 on: April 17, 2014, 07:02:13 am »
Yes, I did see that.  It states that new versions of Lazarus can use the rc and res formats and no longer need lazarus specific format res files.
http://wiki.lazarus.freepascal.org/Lazarus_Resources#FPC_resources
but that doesn't give an example of loading and assigning a PNG resource.

It does say you don't need to precompile the res file yourself, so I am only including the rc file and letting lazarus compile the res at compile time.

I also tried
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
var png:TPortableNetworkGraphic;
begin
     png:=TPortableNetworkGraphic.Create;
     try
        Png.LoadFromResourceName(Hinstance,'logo');
        Image1.Picture.Graphic := Png;
     finally
        Png.Free;
     end;
end;
which still gives the resource error.

logo.png definitely exists and is in the same folder as the rest of the project and rc file.

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Using PNG images inside resource files in Lazarus
« Reply #3 on: April 17, 2014, 07:31:15 am »
make the lrs file:
Code: [Select]
$ /usr/share/lazarus/1.0.14/tools/lazres ./MyFilelrs.lrs ./logo.pngYou can load the image direct to a TPicture object;

and you must load the lrs file

Code: [Select]
procedure TMyForm.ReadMyLogo;
begin
  Image1.Picture.LoadFromLazarusResource('logo');
end

Initialization
{$i MyLrsFIle.lrs}

« Last Edit: April 17, 2014, 02:27:17 pm by bylaardt »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Using PNG images inside resource files in Lazarus
« Reply #4 on: April 17, 2014, 08:53:45 am »
Quote
png definitely exists and is in the same folder as the rest of the project and rc file.
PNG must be in *.lrs file. bylaardt is right.
I use this code.
Code: [Select]
var Glyph: TPortableNetworkGraphic;
begin
...
Glyph := TPortableNetworkGraphic.Create;
Glyph.LoadFromLazarusResource('image_name');       
...
Canvas.Draw(x, y, Glyph);
+ how to load PNG to LRS (I have script for it).
Code: [Select]
#!/bin/bash

~/Lazarus_Qt/lazarus/tools/lazres project_name.lrs image_name.png 
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

JR1234

  • New Member
  • *
  • Posts: 11
Re: Using PNG images inside resource files in Lazarus
« Reply #5 on: April 22, 2014, 11:47:44 pm »
I must be doing something wrong.  Using the lazarus method (if as the wiki said I can use the precompiled RES and RC formats from Delphi I would love to see example code in Lazars loading a PNG res)

I first built the lrs file using the syntax

Code: [Select]
C:\code\Lazarus\Resource Test>c:\lazarus\tools\lazres.exe images.lrs logo.png
logo.png ResourceName='logo' Type='PNG'

The lrs file has this format

Code: [Select]
LazarusResources.Add('logo','PNG',[
  #137'PNG'#13#10#26#10#0#0#0#13'IHDR'#0#0#1#144#0#0#0#254#8#2#0#0#0#155'T'#196
  +#5#0#0#0#9'pHYs'#0#0#11#19#0#0#11#19#1#0#154#156#24#0#0#0#4'gAMA'#0#0#177#142
  +'|'#251'Q'#147#0#0#0' cHRM'#0#0'z%'#0#0#128#131#0#0#249#255#0#0#128#233#0#0
  +'u0'#0#0#234'`'#0#0':'#152#0#0#23'o'#146'_'#197'F'#0#3'V'#28'IDATx'#218'TS'
  +#203'n'#28'E'#20#173'g?gz'#30'=c;'#227'ql'#136#29#147'Hq,!$X'#177'd'#143#196
  +#142'5'#27#190#137#29'[$~ '#192#2#1#18'B d'#130'B'#28#219'!'#142#205'x'#30'='
  +'='#253#174'''w'#28'6'#148'Z'#173'['#183'n'#157':u'#207')'#252#233#231'G'#171

Here is the simplest code I can test it with.  Single form with a single image on it.

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 FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{$i images.lrs}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var png:TPortableNetworkGraphic;
begin
     png:=TPortableNetworkGraphic.Create;
     try
        Png.LoadFromLazarusResource('logo');
        Image1.Canvas.Draw(0,0,Png);
     finally
        Png.Free;
     end;
end;

end.

Now when I try and run the app I get the images.lrs shwoing with this error

Code: [Select]
images.lrs(1,1) Fatal: Syntax error, "BEGIN" expected but "identifier LAZARUSRESOURCES" found
That was using {$i images.lrs}.  If I use {$r images.lrs} I get

Code: [Select]
windres: C:/code/Lazarus/RESOUR~1/images.lrs:1: syntax error
after the compiler or windres tries to compile the lrs file

Any other ideas?  Ideally I would like to use the existing Delphi compiled res file as is supposedly supported, but even getting the lrs method working would be good now.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Using PNG images inside resource files in Lazarus
« Reply #6 on: April 23, 2014, 12:03:10 am »
Try to move {$I images.lrs} to initialization section.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

JR1234

  • New Member
  • *
  • Posts: 11
Re: Using PNG images inside resource files in Lazarus
« Reply #7 on: April 23, 2014, 12:21:10 am »
OK, I created the initialization section just before the final end, ie

Code: [Select]
implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var png:TPortableNetworkGraphic;
begin
     png:=TPortableNetworkGraphic.Create;
     try
        Png.LoadFromLazarusResource('logo');
        Image1.Canvas.Draw(0,0,Png);
     finally
        Png.Free;
     end;
end;

initialization

{$i images.lrs}

end.

and got this error

Code: [Select]
images.lrs(1,17) Error: Identifier not found "LazarusResources"
If I moved the initialization after the final end.  I get the original resource not found error.

Where should the initialization section go?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Using PNG images inside resource files in Lazarus
« Reply #8 on: April 23, 2014, 12:26:23 am »
read and pay attention to http://wiki.lazarus.freepascal.org/Lazarus_Resources it has all the information you need to use lazarus resources.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

JR1234

  • New Member
  • *
  • Posts: 11
Re: Using PNG images inside resource files in Lazarus
« Reply #9 on: April 23, 2014, 01:00:02 am »
I was referring to that wiki.  I had 2 issues/problems with my code,

1. I did not specifically include LResources in the uses clause
2. I had lowercase $i rather than $I

Final code to hopefully help the next person having trouble with this

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

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

type

  { TForm1 }

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

var
  Form1: TForm1;


implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var png:TPortableNetworkGraphic;
begin
     png:=TPortableNetworkGraphic.Create;
     try
        Png.LoadFromLazarusResource('logo');
        Image1.Canvas.Draw(0,0,Png);
     finally
        Png.Free;
     end;
end;

initialization

{$I images.lrs}

end.

Thanks.

                                                 


taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Using PNG images inside resource files in Lazarus
« Reply #10 on: April 23, 2014, 01:36:41 am »
pascal is a case insensitive language.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

JR1234

  • New Member
  • *
  • Posts: 11
Re: Using PNG images inside resource files in Lazarus
« Reply #11 on: April 23, 2014, 02:04:11 am »
Is there any chance you can help with using the Delphi BRCC32 generated res file?  It would make my cutover from Delphi to Lazarus so much simpler.

Projects->Options->Misc shows "FPC resources" checked and the highlight of it says "FPC 2.4 and above required. Like Delphi resources". I have latest Lazarus with FPC 2.6.2 so there is no reason it should be failing using a BRCC32 res file?

The wiki also says after FPC 2.4 the Delphi generated res files can be used directly and states to refer to a compiled res file use $R to refernce the res directly and not the rc.

But this refuses to find the logo resource with the EResNotFound exception stating Resource "logo" not found.

Code: [Select]
{$R images.res}

procedure TForm1.FormCreate(Sender: TObject);
var png:TPortableNetworkGraphic;
begin
     png:=TPortableNetworkGraphic.Create;
     try
        Png.LoadFromLazarusResource('logo');
        Image1.Canvas.Draw(0,0,Png);
     finally
        Png.Free;
     end;
end;

The images.res comes directly from a Delphi project so I know it contains the valid logo resource.


taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Using PNG images inside resource files in Lazarus
« Reply #12 on: April 23, 2014, 02:09:59 am »
sure attach a simple demo that show the problem with the res file and I'll try to find the reason why it does not work.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

JR1234

  • New Member
  • *
  • Posts: 11
Re: Using PNG images inside resource files in Lazarus
« Reply #13 on: April 23, 2014, 04:17:47 am »
OK, attached is the simplest example I could make.
The rc file was compiled to the res using brcc32.
When the project is run it gives the EResNotFound exception.
Thanks for any help you can give with this one.

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: Using PNG images inside resource files in Lazarus
« Reply #14 on: April 23, 2014, 11:00:42 am »
I also had problems when trying to use png graphics in a combobox. I changed to bitmaps. The size increased from about 166 bytes to 1400 bytes. But it worked. I have posted most of my code in my current Combobox Appearance topic http://forum.lazarus.freepascal.org/index.php/topic,24337.msg146450/topicseen.html#new

Hope it may help.

[Edit] Fixed problem with TImagelist and png images. (See above link.) If your graphics are small, it may be easier than using a resource file.
« Last Edit: April 23, 2014, 06:37:59 pm by Windsurfer »

 

TinyPortal © 2005-2018