Solved

Adding a resource inside application.

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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