[SOLVED] Resource files in Lazarus lazarus / 39-Graphics ---------------------------------------------------------------------- PatBayford · 2018-05-21 22:17 How do I extract individual bitmaps from a loaded resource file please? Used to be easy using the Windows API LoadBitmap function, but Lazarus does not support this, presumably as part of the multi- platform drive. In fact, I would like to get hold of a good reference to Resource file use in Lazarus - anybody come across one? ---------------------------------------------------------------------- wp · 2018-05-21 22:39 Quote from: PatBayford on May 22, 2018, 12:17:18 amHow do I extract individual bitmaps from a loaded resource file please? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  pic: TPicture;begin  pic := TPicture.Create;  pic.LoadFromResourceName(HINSTANCE, 'BTN_YES');   // <---- read the resource of the program  Image1.Picture.Assign(pic);  pic.Free;end; ---------------------------------------------------------------------- PatBayford · 2018-05-22 00:37 Thanks wp - came up with that route myself - works fine. Just wanted to make sure there was not some other, more direct route. ---------------------------------------------------------------------- wp · 2018-05-22 07:20 Quote from: PatBayford on May 22, 2018, 02:37:57 amThanks wp - came up with that route myself - works fine. Just wanted to make sure there was not some other, more direct route. More direct than a one-liner? ---------------------------------------------------------------------- Thaddy · 2018-05-22 08:00 And it deserves a link to https://www.youtube.com/watch?v=b19PcuJsQbA ---------------------------------------------------------------------- ccrause · 2018-05-22 09:30 Quote from: PatBayford on May 22, 2018, 02:37:57 amJust wanted to make sure there was not some other, more direct route. An alternative route to working with resources: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  Resource: TResourceStream;begin  Resource := TResourceStream.CreateFromID(HInstance, ResID, PChar(ResourceName));  // the resource is now in a stream, so one can assign, save, read etc.  Resource.SaveToFile('ImportantData.bin');...This gives you direct access (AFAIK, there may be exceptions) to all resources compiled into the application. ---------------------------------------------------------------------- Thaddy · 2018-05-22 10:26 There used to be exceptions(like Windows only...) , but there aren't any anymore (afaik). Resources are even handled correctly cross- platform.(afaik) ---------------------------------------------------------------------- torbente · 2021-12-02 22:47 Sorry for push this old topic, but i search and was unable to find nothing: I developed a custom procedure to get resources and save it to a file: (the file included in the resources were added via Project->Options->Resources->Add Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure CreateFileFromResource(resourcename,filename:string);var  Resource: TResourceStream;begintry   Resource := TResourceStream.Create(HInstance, resourcename, RT_RCDATA);  Resource.Position := 0;  Resource.SaveToFile(filename);finally  Resource.Free;end; End; This only works if i include the unit windows in the Uses section. If not, i get the error: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i nggmainform.pas(45,63) Error: Identifier not found "RT_RCDATA" Any fix that could work cross-platform? Thanks a lot ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-12-02 23:04 Hey Torbente, Quote from: torbente on December 02, 2021, 11:47:58 pmAny fix that could work cross-platform? After following the white rabbit through the hole that is the fpcsrc I found out that: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  RT_RCDATA = MAKEINTRESOURCE(10); So obviously, I then tried to find where MAKEINTRESOURCE was and found out that it just a new type: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  MAKEINTRESOURCE = pchar; So you can now remove the dependency from the Windows unit and change your code to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure CreateFileFromResource(resourcename,filename:string);const  RT_RCDATA = PChar(10);var  Resource: TResourceStream;begintry   Resource := TResourceStream.Create(HInstance, resourcename, RT_RCDATA);  Resource.Position := 0;  Resource.SaveToFile(filename);finally  Resource.Free;end; After that, it will work cross-platform. Cheers, Gus ---------------------------------------------------------------------- Sieben · 2021-12-02 23:20 RT_RCDATA is also defined in LCLType: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$ifdef windows}  RT_CURSOR = Windows.RT_CURSOR;  RT_BITMAP = Windows.RT_BITMAP;  //...  RT_RCDATA = Windows.RT_RCDATA;  //...{$else}  RT_CURSOR = TResourceType(1);  RT_BITMAP = TResourceType(2);  //...  RT_RCDATA = TResourceType(10);  //... ---------------------------------------------------------------------- circular · 2021-12-03 07:14 As a side note, BGRABitmap makes it easy to access resources, wether they are imported from LRS or RES files. The unit BGRABitmapTypes exposes BGRAResource instance that can return a resource by its filename: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  { TBGRAResourceManager }   TBGRAResourceManager = class  protected    function GetWinResourceType(AExtension: string): pchar;  public    function GetResourceStream(AFilename: string): TStream; virtual;    function IsWinResource(AFilename: string): boolean; virtual;  end; var  BGRAResource : TBGRAResourceManager; You can use it as: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i implementation {$R *.lfm} uses BGRABitmap, BGRABitmapTypes, LResources; { TForm1 } procedure TForm1.FormPaint(Sender: TObject);var bmp: TBGRABitmap;begin  bmp := TBGRABitmap.Create;  bmp.LoadFromResource('shape.png');  bmp.Draw(Canvas, 0,0, False);  bmp.Free;end; initialization {$I myresource.lrs} // contains "shape.png" end. When using RES resources, the extension is generally ignored because it is not stored as such in the RES file, but I find it still useful to indicate what kind of data it contains. LazPaint can save images directly into LRS or RES files (they are treated as folders). ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-12-03 13:38 Hey Sieben, Quote from: Sieben on December 03, 2021, 12:20:37 amRT_RCDATA is also defined in LCLType: That's awesome to know. I think I may update the wiki's Lazarus Resources page to make that clear. I think a lot of the confusion comes from that example not mentioning that LCLTypes is better than including Windows. I thank you OH SO very much for a better rabbit hole diving experience than mine Cheers, Gus ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-12-03 18:30 Hey Y'all, I've edited the example on Lazarus Resources, paragraph "Using resources in your program". I've replaced the Windows unit by the LCLType one on the example and removed now obsolete comments. Hopefully, this will make the example fully cross-platform. Cheers, Gus