TI3334 - Extracting a JPEG Resource from an EXE
- Product: Delphi
- Version: All
- Platform: Windows/Win32
Q: How do I extract a JPEG resource from an EXE?
A: This project demonstrates how to extract a resource called
MYJPEG from your EXE:
Pick any JPEG you want to link into your project and copy it to your
project directory. Now, in the same directory, create an RC file
(resource script). Call it MYRES.RC and have it contain this line:
MYJPEG RCDATA YourJpegHere.JPEG
Now, compile this RC file. At the command line, say:
BRCC32 MYRES
This'll create MYRES.RES which you can link into your project.
Now, start a brand new project in Delphi 3. Drop a TButton and a
TImage on the form. Then double click on the button. Then replace the
code inbetween "{$R *.DFM}" and "end." in Unit1 with this code:
{$R MyRes.RES}
uses
JPEG;
const
ResName = 'MYJPEG';
procedure TForm1.Button1Click(Sender: TObject);
var
MyRes : Integer;
MyResP : Pointer;
MyResS : Integer;
MyMS : TMemoryStream;
OldDir : String;
begin
Caption := 'Please wait...';
Application.ProcessMessages;
GetDir(0,OldDir);
ChDir(ExtractFilePath(Application.ExeName));
MyRes := FindResource(HInstance,PChar(ResName),RT_RCDATA);
if MyRes <> 0 then begin
MyResS := SizeOfResource(HInstance,MyRes);
MyRes := LoadResource(HInstance,MyRes);
if MyRes <> 0 then begin
MyResP := LockResource(MyRes);
if MyResP <> nil then begin
MyMS := TMemoryStream.Create;
with MyMS do begin
Write(MyResP^,MyResS);
Seek(0,soFromBeginning);
Image1.Picture.Graphic := TJPEGImage.Create;
Image1.Picture.Graphic.LoadFromStream(MyMS);
Free;
end;
UnLockResource(MyRes);
with Image1.Picture do
Caption := 'Cool JPEG! ('+
IntToStr(Width)+'x'+IntToStr(Height)+')';
end else
Caption := 'LockResource failed. '+
SysErrorMessage(GetLastError);
FreeResource(MyRes);
end else
Caption := 'LoadResource failed. '+
SysErrorMessage(GetLastError);
end else
Caption := 'FindResource failed. '+SysErrorMessage(GetLastError);
ChDir(OldDir);
end;