Resource png not found - delphi

i make a png resource file named glyfs.rc
GLYF_CONFEDITOR RCDATA confeditor.png GLYF_EXTRAFE RCDATA extrafe.png .......
i add it in my project.
Put a TsBitBtn (it is an alphaskin component class) and try to load the glyf from resource file
sbitbtn1.Glyph.LoadFromResourceName(HInstance,'GLYF_CONFEDITOR');
when i run i get the error resource file with name 'GLYF_CONFEDITOR' not found.
What am i doing wrong?
The 3 party tool i use for png is pngdelphi downloaded from here
But the sbitbtn loads native the png files...

There are multiple problems with your code:
TBitBtn.Glyph is of type TBitmap and TBitmap.LoadFromResourceName assumes RT_BITMAP resource type, not RT_RCDATA, hence "resource not found" error.
Even if you use RT_BITMAP it will throw EInvalidGraphic or similar¹ because again - TBitBtn.Glyph is TBitmap and naturally TBitmap wont load PNG data.
¹ there is WinAPI function LoadImage behind LoadFromResourceName, actual error message may vary.

Related

Delphi 11.1 is not adding my RC file during a build

I am trying to add additional icons into my project using Delphi 11.1.
I have produced the .rc file with links to the icon files, e.g:
FILES ICON "D:\Documents\Embarcadero\Studio\Projects\Positron Studio\Icons And Images\PositronFiles.ico"
PROJECTS ICON "D:\Documents\Embarcadero\Studio\Projects\Positron Studio\Icons And Images\PositronProjectFiles.ico"
I have added to the .dpr file:
{$R 'PSResource.res' 'PSResource.rc'}
But, on doing a full build, I get messages like this:
[dcc64 Hint] H2161 Warning: Duplicate resource: Type 14 (ICON GROUP), ID FILES; File D:\Documents\Embarcadero\Studio\Projects\Positron Studio\PSResource.res resource kept; file D:\Documents\Embarcadero\Studio\Projects\Positron Studio\PSResource.res resource discarded.
[dcc64 Hint] H2161 Warning: Duplicate resource: Type 14 (ICON GROUP), ID PROJECTS; File D:\Documents\Embarcadero\Studio\Projects\Positron Studio\PSResource.res resource kept; file D:\Documents\Embarcadero\Studio\Projects\Positron Studio\PSResource.res resource discarded.
The resulting .res file does not include these extra resources.
I have also tried adding the files directly into the resource file using a Resource Editor. This ensures the resources are in the .exe file, but I cannot access them when I use Icon.LoadFromResourceName(Handle,'NAME'), where Handle is to the open window.
Am I doing something wrong?
EDIT:
I tried using the "Project > Resources and Images" dialog.
I removed all references to the .rc file and the file itself. Added the icons I wanted to add, did a build, and now get an error:
[dcc64 Error] E2161 Error: RLINK32: Unsupported 16bit resource in file "D:\Documents\Embarcadero\Studio\Projects\Positron Studio\PositronStudio.dres"
The icon files comprise RGB/A images at resolutions of 64x64, 48x48, 40x40, 32x32, 24x24, 20x20, 16x16, and RGB 256 at 48x48, 23x23, 24x24, 16x16.
Are any of these unsupported?
I removed and reloaded the icons in "Resources and Images". Tried again and did a full build. The resulting .res file only contains the application icon.
I still get the warning
[dcc64 Hint] H2161 Warning: Duplicate resource: Type 24 (user-defined), ID 1; File D:\Documents\Embarcadero\Studio\Projects\Positron Studio\PositronStudio.res resource kept; file D:\Documents\tmssoftware\TMS VCL UI Pack\WINXP.RES resource discarded.
I renamed all the WINXP.* files, and removed all references within the project. Now I don't get the warning of duplicate resource files, but the resulting resource file after a full build still doesn't contain the added icons.
Checked in .dproj for .rc and RCCompile, neither were found.
After removing all the superfluous rc and res files I edited the res file to include the additional items I needed then did a build. This time the res file wasn't overwritten by the compiler. They are now embedded in the exe and I can access them in the application.
It has uncovered another issue in that I cannot change the Icon of a form at runtime. I will raise it as separate topic if I cannot find the solution.

Transparent bitmaps as resources?

I want to compile a resource file with brcc32 that has a 32bit Bitmap and I receive an error message: Error: Invalid bitmap format. If I save the bitmap in 24bit format, it is copiled successfully, but I lose transparency... It is really not possible to have transparent bitmap as resources or I miss something ?
brcc32 does not support creating 32bit BITMAP resources. So either
use a different resource compiler/editor that supports 32bit bitmaps.
create the bitmap resource as an RCDATA resource instead of a BITMAP resource.

How cand I include a bitmap in my custom component (if is possible)?

I am writing a component (a button) which needs a bitmap to be displayed on it. I don't want to make an ImageList property and the user assigns an image. I want that button to have only the image chosen by me.
I tried to include the bitmap in a resource file but when I try to access it I get "Resource not found" error message. This is what I've done:
myres.rc
FIXED BMP "fixed.bmp"
I compiled the resource file with: brcc32 myres.rc
Then I included it in my component unit...
implementation
{$R .\resources\myres.res}
And access it with...
MyComponent.Glyph.LoadFromResourceName(HInstance,'FIXED');
// MyComponent = class(TSpeedButton)
Edit1:
I deleted the {$R .\resources\myres.res} directive and I loaded the resource from menu Project -> Resources and it's working, both with HInstance or FindClassHInstance(MyComponent).
Using a resource editor I found that when I load the resource from the menu the resource appears with the name "FIXED" as it should, but when I load the resource compiled with brcc32 it appears with the name "0". It seems that brcc32 doesn't set the name correctly.
But I don't want to load it from menu, I want it to be loaded automatically with the component.
Edit2:
Remy Lebeau is correct. I was using a wrong BMP format (the file starts with 'BM6' characters instead 'BM8' like Photoshop produce it, and it works).
Change BMP to BITMAP in your RC file, and change HInstance to FindClassHInstance() in your code:
FIXED BITMAP "fixed.bmp"
Glyph.LoadFromResourceName(FindClassHInstance(MyComponent), 'FIXED');

How do I use a custom AVI with TAnimate?

I have an AVI file that I pulled from shell32 using Resources Extract. I would like to use this with TAnimate but I can't figure out how to load this file.
I succesfully loaded the AVI into a .RES file using DelphiDabbler's rcdatacreator program (you have to download the "worked example" to get rcdatacreator. However, my issue now is figuring out how to extract the AVI file from the .RES and supplying it to TAnimate.
I am using Delphi 2010:
Any help is appreciated.
As Andreas mentioned (in his now deleted answer), you don't need to use an external tool to add the resource in recent versions of Delphi.
Use Project/Resources and Images... from the IDE menu. Add a new resource by browsing to the folder your .AVI file is in, give it a name, and type in AVI for the Resource Type. (It's not in the list, but you can add it.)
At runtime, use the following code:
// I used CoolAVI as the resource name in my image above,
// so that's the name I need to use here.
Animate1.ResName := 'COOLAVI';
Animate1.Active := True;

How I Compile Resources into my Application and Access them?

How can I make a single executable package that contains DLL and Image Resource Files?
Then how do I extract them from my Executable at Runtime?
Option 1 using the IDE (Delphi 2007 or Higher):
You can click the Project menu, then select Resources..., which you can load any file into. For your purpose this would be RC_DATA.
Option 2 without the IDE
If you do not have the above option, you will need to use the BRCC32 (Borland Resource Compiler) to create a .RES file from RC file, which you then link to your Application. To link Resource files without using the IDE, try the following:
Lets say for example we want to add a a couple of DLL files, and the name of the DLL files are MyLib1.dll and MyLib2.dll, to add this open Notepad, and type the following:
MYLIB1 RCDATA "..\MyLib1.dll"
MYLIB2 RCDATA "..\MyLib2.dll"
Make sure the ..\xxx.dll paths are correct, so obviously you need to edit that.
Now you need to save this as a .rc file, so File>Save As..(make sure the dropdown filter is All Files .) and name it MyResources.rc. Now you need to use the Resource Compiler to generate the Res file, using this console command:
BRCC32 MyResources.RC
You can write that command by using the Command Prompt, Start Menu > Run > cmd.exe, alternatively you can find the BRCC32.exe inside the bin folder of your Delphi setup and drag the MyResource.RC file onto.
This will create a Res file named MyResources.RES which you can include inside the Main Delphi form of your Application, like so:
{$R *.dfm}
{$R MyResources.res}
you can extract the resources by using something like this:
procedure ExtractResource(ResName: String; Filename: String);
var
ResStream: TResourceStream;
begin
ResStream:= TResourceStream.Create(HInstance, ResName, RT_RCDATA);
try
ResStream.Position:= 0;
ResStream.SaveToFile(Filename);
finally
ResStream.Free;
end;
end;
What I've found out to be convenient, is to use a .zip container.
Then you'll have two implementations:
Append some .zip content to an existing .exe, and the .exe code will retrieve the .zip content on request;
Embed the .zip content as a resource, then extract on request each content.
Solution 1 will add the .zip content after compilation. Whereas 2 will add the .zip content at compilation. For a setup program, I think solution 1 makes sense to me. For a way of retrieving some needed files (libraries, and even bitmaps or text) which are linked to a particular exe release, solution 2 could be envisaged.
Using .zip as format make it easy to parse the content, and allow compression. Using a tool like TotalCommander, you can even read the .zip file content with Ctrl+PgDown over the .exe. Very convenient.
You'll find in this link how you implement solution 1, and in this link (same page, but another post) how to use the TZipRead.Create() constructor to directly access to a .zip bundled as resource. You'll find in our repository how it works with working applications: e.g. how we embedded icons, textual content and graphviz + spell-checker libraries in the SynProject executable.
About performance, there is no difference between the two solutions, at least with our code. Both use memory mapped files to access the content, so it will be more or less identical: very fast.

Resources