Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding a resource inside application.

Posted on 2003-11-06
3
Medium Priority
?
654 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
Ray_Adams earned 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

715 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