LoadMemory example for Delphi
This example uses Windows API calls to load a file into memory, then uses the LoadMemory method to load the memory-resident file into a bitmap.
procedure TForm1.Button1Click(Sender: TObject);
var
hf: HFILE;
hMem: integer;
lpMem: pointer;
iSize: Longint;
label quit_loadmemory;
begin
{ Load the file into memory.}
hf := FileOpen('c:\lead\images\image1.cmp', fmOpenRead);
If hf = HFILE_ERROR Then
begin
MessageDlg ('Error opening file!', mtError, [mbOK], 0);
goto quit_loadmemory;
End;
iSize := FileSeek(hf,0,2);
FileSeek(hf, 0, 0);
hMem := GlobalAlloc(GMEM_MOVEABLE,iSize);
if hMem = 0 then
begin
FileClose (hf);
MessageDlg ('Not enough memory to hold the file in memory!', mtError, [mbOK], 0);
goto quit_loadmemory
end;
lpMem := GlobalLock(hMem);
If lpMem = nil Then
begin
FileClose (hf);
GlobalFree(hMem);
MessageDlg ('Not enough memory to hold the file in memory!', mtError, [mbOK], 0);
goto quit_loadmemory
end;
if FileRead(hf, lpMem^, iSize) < iSize Then
begin
FileClose (hf);
GlobalUnlock(hMem);
GlobalFree(hMem);
MessageDlg ('Error reading file!', mtError, [mbOK], 0);
goto quit_loadmemory
end;
GlobalUnlock(hMem);
FileClose (hf);
{Load the bitmap from the memory-resident file.}
if Lead1.LoadMemory(hMem, 0, 0, iSize) <> SUCCESS Then
MessageDlg ('Error calling LoadMemory', mtError, [mbOK], 0)
else
MessageDlg ('LoadMemory succeeded', mtInformation, [mbOK], 0);
GlobalFree (hMem);
quit_loadmemory:
end;