Solved

Adding a resource inside application.

Posted on 2003-11-06
3
638 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now