Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ResStream problem

Status
Not open for further replies.

MLNorton

Programmer
Nov 15, 2009
134
US
I am trying to learn how to imbed Graphics in an executable Delphi program. I have created a RES fil;e that contains two graphics. I have created a Procedure that I found on the Internet as follows:

implementation

{$R *.dfm}
{$R MyPic.RES}

procedure TForm_Main.Button_PrintClick(Sender: TObject);
Var
MyPic: TBitMap;
: TResourceStream;
begin
try
MyPic := TBitMap.Create;
ResStream := TResourceStream.CreateFromID(hinstance, 1, RT_RCDATA);
MyPic.LoadfromStream(ResStream);
MyPic.Canvas.Draw(15,15, MyPic);
finally
MyPic.Free;
end;
end;

The program Compiles but fails on Run with the following error message on the highlighted line:

“Project TestPrint.exe raised exception class EResNotFound with Message Resource 1 not found.”
Can anybody help me solve this problem?
 
1. If you are using the RC file you posted in you previous thread, you are trying to access a named resource by index. You need to use the Create Method and pass the bitmap's name to the funcion. Also, you should be using resource type Bitmap, not RCData as follows:
Code:
   ResStream := TResourceStream.CreateFromID(hinstance,'BRIGADE', RT_BITMAP);

Now when I made all those changes, I started getting an unknown bitmap format exception in my test app; I am not sure yet, but this may be due to incompatibilities with my particular resource editor and have nothing to do with yours. Try the changes I have suggested and let us know what happens
 
I corrected the line you identified and got an error message:
[DCC Error] Main.pas(38): E2010 Incompatible types: 'Integer' and 'string'

What next?
 
Dang it, wish there was way to edit a post after you send it. It should be TResourceStream.Create, not CreateFromID.
 
I made the correction and the program now has the message "Resouce Brigade not found".
From this I expect there is a problem with the MyPic.RES file. I have repeatedly run BRCC32 PyPic.RC and continually gotten the same resulte. When I created the MyPic.RC file I use the following in the Command Prompt to change the file extension, "rem MyPic.txt MtPic.RC'.

What next?
 
I have been working on this all night; I do not use resource scripts very often, so I had to do some research why this was not working. This is what I have come up with. Notice the Null Handle being passed to LoadFromResourceName; I found out if you try to pass it the Application.Handle, it will fail. If you pass it a null pointer, it will search the current module for the resource.

Code:
//interface code goes here
implementation

{$R *.dfm}
{$R MyPic.res}

procedure TForm1.Button1Click(Sender: TObject);
Var
 MyPic: TBitMap;
 begin
   try
     MyPic := TBitMap.Create;
     MyPic.LoadFromResourceName(0, 'BITMAP_1');
     Image1.Picture.Bitmap:=MyPic;
   finally
     MyPic.Free;
   end;
 end;

I also put a TImage comoponent called Image1 on my form to dispay the bitmap, instead of drawing directly to the form.

Working my way out from that, I think if you pass TResourceStream.Create 0 for the hInstance parameter, Your code should work. Or you could use TBitmap's own methods to load the resource as I did in the code above.
 
I made changes, placed Image1 on the form and ran program with the following error message:
Project TestPrint.exe raised exception class EResNotFound with message 'Resource BitMap not found'.

What next?
 
That is because your resource script does not have a resource named BITMAP_1. IIRC, Your script resources are named Brigade and SigFlags. Replace BITMAP_1 with the desired resource name.
 
Made correction, changed BitMap_1 to Brigade. 'Resource BitMap not found'.
The problem seems to be with the RES file. I have created MyPic.RES several times.

How can I verify that the RES fil is good?
 
1. What version of delphi are you using?
2. Post the contents of your RC file here.
3 Are you using BRCC32.EXE to compile the RC file or RC.EXE?
4. Are you using the Command line to compile it? If so, post the Commandline arguments. If not, copy and paste the command line from the messages window.
 
Delphi XE2

Brigade bmp "Brigade.bmp"
SigFlags bmp "SigFlags.bmp"

BRCC32.exe

BRCC32 MyPic

The RES file is created corectly. I found a RES viewer program, "Resource Hacker", which displays both of my images corectly.
 
You need to change the resource type from BMP to Bitmap in the Resource script. The LoadResource functions are expecting a bitmap resource to be under the BITMAP section, and instead your script is putting them under a custom type called BMP and Windows doesn't know to look there for them. If you get an error under BRCC32 of "Fatal Error Expecting resource name or type" like I do, try compiling using RC.EXE. This is Microsoft's resource compiler.
 
PROBLEM SOLVED. THANKS FOR ALL OF THE WORK YOU DID.

I have one more request. I need to print to the Canvas. Can you provide me the changes to print to the canvas rather than to the screen? Once I can ptint to the Canvas I am home free.
 
To draw on a forms canvas, just draw on it.

Code:
procedure TForm1.Button1Click(Sender: TObject);
Var
 MyPic: TBitMap;
 begin
   try
     MyPic := TBitMap.Create;
     MyPic.LoadFromResourceName(0, 'Brigade');
     Canvas.Draw(207, 27, MyPic);
   finally
     MyPic.Free;
   end;
 end;
 
Thanks for your reply but I resolved the problem myself and it works with no problem. I have incorporated the solution an existing program that reads the graphics from a graphics folder. The following are my changes:

procedure TForm_Main.Button_PrintClick(Sender: TObject);
Var
MyPic: TBitMap;
ResStream: TResourceStream;
begin
try
MyPic := TBitMap.Create;
with Printer do
begin
BeginDoc;
MyPic.LoadFromResourceName(0, 'Brigade');
Printer.Canvas.Draw(3,3,MyPic);
MyPic.LoadFromResourceName(0, 'Flags');
Printer.Canvas.Draw(3000,3,MyPic);
EndDoc;
end;
finally
MyPic.Free;
end;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top