Recent

Author Topic: (solved)Issue with resources  (Read 984 times)

ShungFeng

  • New Member
  • *
  • Posts: 20
(solved)Issue with resources
« on: February 20, 2020, 01:37:21 pm »
Hello guys,

I'm trying to work with resources for the first time, but I think I'm missing something...

I created the resource using glazres, moves the resource file to my project folder, added an initialization section at the bottom of my unit with: "{$I resources.lrs}" in it, added "LResources" to units, then I use the following code to extract the resource to a folder on my computer:

Code: Pascal  [Select][+][-]
  1. procedure ExtractResource;
  2. var
  3.   Resource: TResourceStream;
  4. begin
  5.   Resource := TResourceStream.Create(HINSTANCE, 'bitmapimage', RT_BITMAP);
  6.   Resource.SaveToFile('C:\folder\image.bmp');
  7. end;  

Then when I go and call the procedure, I get the following error:

Quote
Resource "bitmapimage" not found

But my resource file looks like this:

Code: Pascal  [Select][+][-]
  1. LazarusResources.Add('bitmapimage','BMP',[ // Followed by the image data

Why is this not working?
« Last Edit: February 20, 2020, 07:32:39 pm by ShungFeng »

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Issue with resources
« Reply #1 on: February 20, 2020, 01:45:02 pm »
You are mixing old resource functionality with the new FPC style resources.

Please read this wiki article for more info about FPC style resources : https://wiki.freepascal.org/Lazarus_Resources#FPC_resources

ShungFeng

  • New Member
  • *
  • Posts: 20
Re: Issue with resources
« Reply #2 on: February 20, 2020, 02:47:35 pm »
Thank you for that Cyrax, I used lazres to make a .RES file now using the following command:

Quote
lazres resources.res C:\folder\bitmapimage.bmp=IMG1

This created the .RES file, which I then copied to my project folder, converted "{$I resources.lrs}" to "{$R resources.res}", and it ran! It produced the file, which was the exact same filesize as the original, but the bmp that it produces does not work. When I try to open it in GIMP, for example, is says that it's not a valid bmp file, but the original one opens fine.

Why is this?

PS: I used this code to create it:

Code: Pascal  [Select][+][-]
  1. procedure ExtractResource;
  2. var
  3.   ResStream: TResourceStream;
  4. begin
  5.   ResStream := TResourceStream.Create(HInstance, 'IMG1', RT_BITMAP);
  6.   try
  7.     ResStream.Position := 0;
  8.     ResStream.SaveToFile('C:\folder2\bitmapimage.bmp');
  9.   finally
  10.     ResStream.Free;
  11.   end;      
  12. end;
« Last Edit: February 20, 2020, 02:49:32 pm by ShungFeng »

Handoko

  • Hero Member
  • *****
  • Posts: 5150
  • My goal: build my own game engine using Lazarus
Re: Issue with resources
« Reply #3 on: February 20, 2020, 03:03:32 pm »
Do you use Lazarus? Lazarus can import resource files much easier.
Lazarus main menu > Project > Project Options > Resources

ShungFeng

  • New Member
  • *
  • Posts: 20
Re: Issue with resources
« Reply #4 on: February 20, 2020, 03:28:30 pm »
Hi Handoko!

Your right, this is an easier way to do it, and I've changed my code accordingly. But my program still runs into the same problem - it outputs the bmp, and it's the exact same filesize, but unlike the original, this bmp does not open in any editor programs. Is there a maximum filesize a resource can be or something, or any other reason why this bmp might not open after being "SaveToFile"?

Thanks.

Handoko

  • Hero Member
  • *****
  • Posts: 5150
  • My goal: build my own game engine using Lazarus
Re: Issue with resources
« Reply #5 on: February 20, 2020, 03:59:18 pm »
Don't ask me why TResourceStream.SaveToFile does not work but I know this code below works:

Code: Pascal  [Select][+][-]
  1. procedure SaveResource;
  2. var
  3.   Stream: TResourceStream;
  4.   Image:  TImage;
  5. begin
  6.   Stream := TResourceStream.Create(HInstance, 'IMG1', RT_RCDATA);
  7.   Image := TImage.Create(nil);
  8.   Image.Picture.LoadFromStream(Stream);
  9.   Image.Picture.SaveToFile('/home/handoko/Desktop/image.bmp');
  10.   Image.Free;
  11.   Stream.Free;
  12. end;
« Last Edit: February 20, 2020, 04:03:51 pm by Handoko »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Issue with resources
« Reply #6 on: February 20, 2020, 04:01:22 pm »
Most probably your bitmap got converted to a DDB and saved as that. Most programs won't recognize a DDB in a BMP file.

You can try to convert to a DIB by loading it to a properly set TBitmap or TPicture and using MyImage.SaveToFile().

ETA: Yeah, what Handoko coded :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ShungFeng

  • New Member
  • *
  • Posts: 20
Re: Issue with resources
« Reply #7 on: February 20, 2020, 07:32:22 pm »
Yes, thank you handoko, solved now.

 

TinyPortal © 2005-2018