whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

80 Visitors Online


 
...Recover Bitmaps from Resource Files by code?
Autor: Davy Landman
[ Print tip ]  

Tip Rating (8):  
     


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'); }


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners