建一个DLL,什么代码都不要写只要把你放图片的资源文件包含进来就可以了 library MyResource;{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. }uses SysUtils, Classes;{$R Resource.res} //放图片的资源文件 {$R *.res}beginend.
var
Modulehandle:HMODULE;
bmp:Tbitmap;
begin
Modulehandle:=LoadLibrary('shell32.dll');
bmp:=Tbitmap.Create;
bmp.LoadFromResourceID(Modulehandle,130); //假设图片资源ID号为130
FreeLibrary(Modulehandle);
self.Canvas.Draw(0,0,bmp);
bmp.Free;
end;
比如aaa.rc
内容为
130 BITMAP aaa.bmp在写DLL时将此文件添加进去编译再用上面那段程序从DLL中引出
library MyResource;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,
Classes;{$R Resource.res} //放图片的资源文件
{$R *.res}beginend.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons;type
TForm1 = class(TForm)
Image1: TImage;
procedure FormCreate(Sender: TObject);
end;var
Form1: TForm1;
ResourceDLL: THandle = 0;implementation{$R *.dfm}//xResourceName: 资源文件中的资源名 xBMP: 用来接图片
procedure LoadBMP(xResourceName: string; xBMP: TBitmap);
var
mHandle, mMemory: THandle;
mSize: Longint;
mData: PByte;
mBmpHdr: TBitmapFileHeader;
mStream: TMemoryStream;
begin
mHandle := FindResource(ResourceDLL, PChar(xResourceName), RT_BITMAP);
if mHandle = 0 then Exit;
mMemory := LoadResource(ResourceDLL, mHandle);
if mMemory = 0 then Exit;
mData := LockResource(mMemory);
try
mSize := SizeofResource(ResourceDLL, mHandle);
mStream := TMemoryStream.Create;
try
mBmpHdr.bfType := $4D42;
mStream.SetSize(SizeOf(mBmpHdr) + mSize);
mStream.Write(mBmpHdr, Sizeof(mBmpHdr));
mStream.Write(mData^, mSize);
mStream.Seek(0, 0);
xBMP.LoadFromStream(mStream);
finally
mStream.Free;
end;
finally
UnlockResource(mMemory);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
LoadBMP('BMP1', Image1.Picture.Bitmap);
end;initialization
ResourceDLL := LoadLibrary('MyResource.dll');finalization
if ResourceDLL <> 0 then
FreeLibrary(ResourceDLL);
end.