Category: Graphic
Title: Get Bitmaps from Resource Files
Date added: 15.03.2006
Hits: 1746
procedure GetBitmapFromResource(
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;
{How to use:
GetBitmapFromResource('BITMAP_1', 'C:\img.bmp');
}