Question and Answer Database FAQ1882D.txt Using JPEGs from resource files Category :VCL Platform :All Product :All 32 bit Question: How can I load a JPEG image from a resource file that is linked with my application? Answer: The following demonstrates creating a resource file containing a JPEG image, and loading the JPEG file from the resource file. The resulting JPEG image is displayed in a Image component. 1) Create a text file with the extension of ".rc". The text file should be named something different than the project name or any unit name in your application to avoid any confusion for the compiler. The text file should contain the following line: MYJPEG JPEG C:\DownLoad\MY.JPG Where: "MYJPEG" is the name you wish to name the resource "JPEG" is the user defined resource type. "C:\DownLoad\MY.JPG" is the path and filename of the JPEG file. For our example we will name the file "foo.rc" . Now run the BRCC32.exe (Borland Resource CommandLine Compiler) program found in the Delphi/C++ Builders bin directory giving the full path to the rc file: C:\DelphiPath\BIN\BRCC32.EXE C:\ProjectPath\FOO.RC You should now have a compiled resource file named the same as the ".rc" file you compiled with the extension of ".res". The following demonstrates using the embedded JPEG in your application {Link the res file} {$R FOO.RES} uses Jpeg; procedure LoadJPEGFromRes(TheJPEG : string; ThePicture : TPicture); var ResHandle : THandle; MemHandle : THandle; MemStream : TMemoryStream; ResPtr : PByte; ResSize : Longint; JPEGImage : TJPEGImage; begin ResHandle := FindResource(hInstance, PChar(TheJPEG), 'JPEG'); MemHandle := LoadResource(hInstance, ResHandle); ResPtr := LockResource(MemHandle); MemStream := TMemoryStream.Create; JPEGImage := TJPEGImage.Create; ResSize := SizeOfResource(hInstance, ResHandle); MemStream.SetSize(ResSize); MemStream.Write(ResPtr^, ResSize); FreeResource(MemHandle); MemStream.Seek(0, 0); JPEGImage.LoadFromStream(MemStream); ThePicture.Assign(JPEGImage); JPEGImage.Free; MemStream.Free; end; procedure TForm1.Button1Click(Sender: TObject); begin LoadJPEGFromRes('MYJPEG', Image1.Picture); end; 7/16/98 4:31:28 PM
Last Modified: 01-SEP-99