Recent

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

JR1234

  • New Member
  • *
  • Posts: 11
Re: Using PNG images inside resource files in Lazarus
« Reply #15 on: April 24, 2014, 01:25:19 am »
Thanks for the idea, but changing from PNG to BMP still gives the same error.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Using PNG images inside resource files in Lazarus
« Reply #16 on: April 25, 2014, 06:45:06 am »
there are 2 main problems with your code
1) you use LoadFromLazarusResource instead of LoadFromResourceName
2) LoadFromResourcename has no way to define the type of resource you want to load
    It gets the type from the virtual method GetResourceType which is not overridden in the TPortableNetworkGraphic and uses the default result from the TCustomBitmap class which is defined as RT_BITMAP.

In this case I would simple avoid using the build in methods and do it manually. eg
Code: [Select]

implementation
uses LCLType;
{$R *.lfm}
{$R images.res}

{ TForm1 }

procedure TForm1.FormCreate(Sender : TObject);
var
  png : TPortableNetworkGraphic;
  vStream : TResourceStream;
begin
  png:=TPortableNetworkGraphic.Create;
  try
    vStream := TResourceStream.Create(HINSTANCE,'LOGO',RT_RCDATA);
    try
      Png.LoadFromStream(vStream);
      Image1.Picture.Assign(png);
    finally
      vStream.Free;
    end;
  finally
    Png.Free;
  end;
end;

Try to use the above code in your sample project and see for your self. By the way I replaced the draw call with the assign because the png lost its transparency and it was replaced with a black background, this way the transparency is preserved.
« Last Edit: April 25, 2014, 06:47:16 am by taazz »
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 #17 on: April 28, 2014, 01:16:50 am »
Yes, that worked.

Thank you so much.  Once step closer on the road from Delphi to Lazarus.  ;)

kjteng

  • Sr. Member
  • ****
  • Posts: 259
Re: Using PNG images inside resource files in Lazarus
« Reply #18 on: March 09, 2017, 12:12:47 pm »
You shall use LoadFromResourceName method (instead of LoadFromLazarusResource) to load the JPG or PNG image from the .res or .rc file.
 Image1.Picture.PNG.LoadFromResourceName(HINSTANCE, 'logo')

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Using PNG images inside resource files in Lazarus
« Reply #19 on: March 09, 2017, 04:35:30 pm »
Or simply load a tmemorystream from resource and then load the image from the memorystrea,

 

TinyPortal © 2005-2018