INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

ResStream problem

ResStream problem

(OP)
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?

RE: ResStream 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

RE: ResStream problem

(OP)
I corrected the line you identified and got an error message:
[DCC Error] Main.pas(38): E2010 Incompatible types: 'Integer' and 'string'

What next?

RE: ResStream problem

My Mistake; It should be TResource.Create, not CreateFromID.

RE: ResStream problem

Dang it, wish there was way to edit a post after you send it. It should be TResourceStream.Create, not CreateFromID.

RE: ResStream problem

(OP)
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?

RE: ResStream problem

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.

RE: ResStream problem

(OP)
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?

RE: ResStream problem

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.

RE: ResStream problem

(OP)
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?

RE: ResStream problem

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.

RE: ResStream problem

(OP)
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.

RE: ResStream problem

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.

RE: ResStream problem

(OP)
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.

RE: ResStream problem

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; 

RE: ResStream problem

(OP)
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;

RE: ResStream problem

Glad to have been of assistance.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close