Solved

Adding a resource inside application.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

19 Experts available now in Live!

Get 1:1 Help Now