Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to store images in .dll

1,023 views
Skip to first unread message

Alexander Pogodin

unread,
Oct 26, 1999, 3:00:00 AM10/26/99
to
Hi, all!
I have some application, were I want user to select images to represent
different status of orders in TTreeView. So, now I need to store images, but
I don't want to put them into my executable. So, I need to store images
somewhere in DLL, like MicroSoft stores them in moreicons.dll, or maybe
there is some other ways?

Any suggestions will be appriciated.

Thanks, Alex.

Steve Schafer (TeamB)

unread,
Oct 26, 1999, 3:00:00 AM10/26/99
to
On Tue, 26 Oct 1999 14:15:33 +0200, "Alexander Pogodin"
<alex_p...@lycosmail.com> wrote:

>I have some application, were I want user to select images to represent
>different status of orders in TTreeView. So, now I need to store images, but
>I don't want to put them into my executable. So, I need to store images
>somewhere in DLL, like MicroSoft stores them in moreicons.dll, or maybe
>there is some other ways?

Using the resource editor of your choice (e.g., Resource Workshop),
create a .RES file containing the bitmaps, icons, etc. Give each one a
name, and make sure the name is ALL UPPERCASE.

The source code for your resource DLL should look something like this:

library MyImages;

uses
Windows;

{$R MYIMAGES.RES}

begin
end.

Compile this, and you will have your resource DLL.

To use the resource DLL from your application, you have to first load
the DLL, and then load the resource. Something like this:

var
DllHandle: THandle;
Bitmap: TBitmap;
begin
DllHandle := LoadLibrary('MyImages.dll');
if DllHandle <> 0 then
try
Bitmap := TBitmap.Create;
Bitmap.Handle := LoadBitmap(DllHandle, 'MYBITMAP');
// use the bitmap here
Bitmap.Free;
finally
FreeLibrary(DllHandle) end
else
raise Exception.Create(SysErrorMessage(GetLastError)) end;

-Steve


Yoram Halberstam

unread,
Oct 26, 1999, 3:00:00 AM10/26/99
to
To do this you need to include the icons or bitmap as a resource. Use the imaghe
editorshiped with Delphi. Open the "YourDLL.Res" file and draw the images in.

then open your DLL in delphi do build all and here we go...

Dr. Delphi

Alexander Pogodin wrote:

> Hi, all!


> I have some application, were I want user to select images to represent
> different status of orders in TTreeView. So, now I need to store images, but
> I don't want to put them into my executable. So, I need to store images
> somewhere in DLL, like MicroSoft stores them in moreicons.dll, or maybe
> there is some other ways?
>

Wolfgang Hubert

unread,
Oct 27, 1999, 3:00:00 AM10/27/99
to
Alex,

I have found the following article on the Internet (I am sorry, I missed the
URL - but you will find it using www.metacrawler.com):

Including JPEG's In Your Executable

STEP ONE:
Create a resource script file (*.RC) with a simple text editor like Notepad
and add the following line:

1 RCDATA "MyPic.jpg"

The first entry is simply the index of the resource. The second entry
specifies that we are dealing with a user-defined resource. The third and
final entry is the name of the jpeg file.

STEP TWO:
Use Borland's Resource Compiler, BRCC32.EXE, to compile it into a .RES file.
At the MS-DOS command line:

BRCC32 MyPic.RC

This will create a resource file called MyPic.RES.

STEP THREE:
Add a compiler directive to the source code of your program. It should
immediately follow the form directive, as shown here:

{$R *.DFM}
{$R MyPic.RES}

STEP FOUR:
Add the following code to your project (I've created a procedure for it):

procedure LoadJPEGfromEXE;

var
MyJPG : TJPEGImage; // JPEG object
ResStream : TResourceStream; // Resource Stream object

begin
MyJPG := TJPEGImage.Create;
ResStream := nil;
try
ResStream := TResourceStream.CreateFromID(HInstance, 1, RT_RCDATA);
MyJPG.LoadFromStream(ResStream); // What!? Yes, that easy!
Canvas.Draw(12,12,MyJPG); // draw it to see if it really worked!
finally
MyJPG.Free;
ResStream.Free;
end;
end; // procedure

See the second parameter of the CreateFromID procedure of the
TResourceStream object? It's simply the resource index. You can include more
than one jpeg in your executable just by adding a line for each jpeg (with a
different index) in the resource script (.RC) file.

STEP FIVE:
Call the procedure, run the program, and voila! Now go eat some nachos.
This was written by: lone...@healey.com.au

If you want to load an image from a DLL, you will just have to load the DLL
dynamically, and apply its Instance handle (which is returned by
LoadLibrary) to the avove call to TResourceStream.CreateFromID.

Regards, Wolfgang


0 new messages