Solved

Adding a resource inside application.

Posted on 2003-11-06
3
643 Views
Last Modified: 2007-12-19
Hi.

My application at a certain point needs a bitmap file. This bitmap file has to be in the directory where the application starts. If not the application raise an exception.
In the case of accidental deletion of the bitmap file i putted that 'bmp' in a resource file.
I used brcc32.exe.

Brcc32 read my rc file (Image.rc) where i wrote the following :
Image.rc  ===> (Un5 BITMAP Un5.bmp).

Inside the application i tried to call the resource Image to create that bitmap file everytime the program starts in the starting directory.

There is 1 problem.
Although the file creation part is ok. The first 14 bytes are missing (I think the file header). And that thing cause the raise of the exception i mentioned before, (file is unreadable). I need the whole bitmap file to be created and i dont know whats wrong with the code ... i replaced RT_BITMAP with RT_RCDATA and same the same thing happened.

Here is the code i use :
......
uses Unit1;
{$R *.DFM}
{$R Image.RES}
.....

procedure TForm2.FormCreate(Sender: TObject);
var
  fname1: string;
  rStream: TResourceStream;
  fStream: TFileStream;
......
Begin

fname1:=ExtractFileDir(Paramstr(0))+'\Un5.bmp';

TRY
 rStream := TResourceStream.Create(hInstance, 'un5', RT_BITMAP);
       try
        fStream := TFileStream.Create(fname1, fmCreate);
              
            try
               fStream.CopyFrom(rStream, 0);
              finally
               fStream.Free;
              end;

       finally
        rStream.Free;
       end;

EXCEPT on E: Exception do
    Begin
    raise Exception.Create('...Error..');
    End;
End;

end;
0
Comment
Question by:CodedK
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
Ray_Adams earned 50 total points
ID: 9696315
Just created small project with resource.
rvb.rc
rvb BITMAP rvb.bmp
--
I used rc.exe (from VC) to compile it because brcc32 give me the error,
Borland Resource Compiler  Version 5.40
Copyright (c) 1990, 1999 Inprise Corporation.  All rights reserved.

Error rvb.rc 1 16: Invalid bitmap format

--
In delphi

     Image1.Picture.Bitmap.LoadFromResourceName(hinstance,'rvb');

---
if you use resource as RCDATA
rvb RCDATA rvb.bmp
you can use this code

     r:=TResourceStream.Create(hinstance,'rvb',RT_RCDATA);
     Image1.Picture.Bitmap.LoadFromStream(r);
     r.Free;
--

0
 
LVL 16

Author Comment

by:CodedK
ID: 9698010
Thank you Ray_Adams.

First of all, your answer was perfect. I didnt know that i could load directly from resourses without ever the file be created. This saved me from a lot of work.

 But...

But what if i really want to collect some stuff inside an exe file and then the real job of the application is to create files that it stores inside ???

I saw that this can not be done (at least with bitmap files) because the first 14 bytes are missing.
The problem of the first 14 bytes remains. Why this happens.
 ...If you have time and you know something pls answer why this problem occur.Thank you very much.
I will accept your answer and i will rate it with A. I also increased the points to 50, cause not only you gave a perfect answer but you saved me from a lot of work.
0
 
LVL 2

Expert Comment

by:Ray_Adams
ID: 9699264
If you want to store files in your project and then in future extract it to disk, use RCDATA resource type.

 r:=TResourceStream.Create(hinstance,'rvb',RT_RCDATA);
 f.SaveToFile('mybitmap.bmp');
 r.Free;

I did it in my project , but i store device driver, not bitmap, and everything is ok. I can extract resource, save it to file and then install that driver.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question