首页  编辑  

从资源中恢复位图文件

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

从资源中恢复位图文件

procedure RecoverBitmapFromResource(const sRecourceName: PAnsiChar; const sDestFileName: string);

const

 BM = $4D42;  {Bitmap type identifier}

var

 Bmp: TBitmap;

 BMF: TBitmapFileHeader;

 HResInfo: THandle;

 MemHandle: THandle;

 mStream: TMemoryStream;

 ResPtr: PByte;

begin

 BMF.bfType := BM;

 { Find, Load, and Lock the Resource containing BITMAP1 }

 HResInfo  := FindResource(HInstance, sRecourceName, RT_BITMAP);

 MemHandle := LoadResource(HInstance, HResInfo);

 ResPtr    := LockResource(MemHandle);

 { the header is lost, so will need to be recalculated,

   but lets be lazy and let TBitmap recreate the full header }

 mStream := TMemoryStream.Create;

 try

   mStream.SetSize(SizeofResource(HInstance, HResInfo) + SizeOf(BMF));

   mStream.Write(BMF, SizeOf(BMF));

   mStream.Write(ResPtr^, SizeofResource(HInstance, HResInfo));

   mStream.Seek(0, 0);

   {Create the TBitmap and load the image from the MemoryStream}

   Bmp := TBitmap.Create;

   try

     Bmp.LoadFromStream(mStream);

     Bmp.SaveToFile(sDestFileName);

   finally

     Bmp.Free;

   end;

 finally

   FreeResource(MemHandle);

   mStream.Free;

 end;

end;

{usage: RecoverBitmapFromResource('BITMAP_1', 'C:\recover.bmp'); }