首页  编辑  

从资源中读取JPEG

Tags: /超级猛料/Resource.资源和使用/   Date Created:

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;