LoadMemory example for Delphi
This example uses WIN32 API calls to load a file into memory, and then uses the LoadMemory method to load the memory-resident file into a bitmap.
var
hf: Longint; // For WIN32 declare this as Long
hMem: HGLOBAL; // For WIN32 declare this as Long
lpMem: Pointer;
iSize: Longint;
RasterIO: LEADRasterIO;
begin
RasterIO:= CreateComObject(CLASS_LEADRasterIO) as LEADRasterIO;
{ Load the file into memory.}
hf := FileOpen('v:\images\image1.cmp', fmOpenRead);
if (hf <= 0 ) then
begin
MessageDlg ('Error opening file!', mtError, [mbOK], 0);
Exit;;
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);
Exit;
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);
Exit;
end;
if (FileRead(hf, lpMem^, iSize) < iSize) then
begin
FileClose (hf);
GlobalUnlock(hMem);
GlobalFree(hMem);
MessageDlg ('Error reading file!', mtError, [mbOK], 0);
Exit;
end;
GlobalUnlock (hMem);
FileClose (hf);
//Load the bitmap from the memory-resident file.
if (RasterIO.LoadMemory (LEADRasterView1.Raster, hMem, 0, 0, -1, iSize) <> 0) then
MessageDlg ('Error calling LoadMemory', mtError, [mbOK], 0)
else
MessageDlg ('LoadMemory succeeded', mtInformation, [mbOK], 0);
GlobalFree (hMem);
end;