SaveMemory example for Delphi
This example uses SaveMemory to save a bitmap to a memory-resident file, uses GetMemoryInfo to examine the file size; then uses WIN32 API calls to save the file to disk and free the memory.
var
RasterIO: LEADRasterIO;
hf: longint;
hMem: longint;
iSize: longint;
lpMem: Pointer;
cMsg: string;
begin
RasterIO := CreateComObject (CLASS_LEADRasterIO) as ILEADRasterIO;
hMem:= 0;
iSize:= 0;
if (RasterIO.SaveMemory (LEADRasterView1.Raster, hMem, FILE_CMP, 24, 2, 0, iSize) <> 0) then
begin
ShowMessage('Error calling SaveMemory');
Exit;
end;
if (RasterIO.GetMemoryInfo (LEADRasterView1.Raster, hMem, 0, iSize) <> 0) then
begin
GlobalFree (hMem);
Exit;
end;
//Ask for a confirmation to save this file
cMsg:= 'File info:' + Chr(13);
cMsg:= cMsg + IntToStr(Trunc(RasterIO.InfoWidth)) + ' x ' + IntToStr(Trunc(RasterIO.InfoHeight));
cMsg:= cMsg + ' x ' + IntToStr(RasterIO.InfoBits) + ' BPP' + Chr(13);
cMsg:= cMsg + 'Size in memory: ' + IntToStr(RasterIO.InfoSizeMem) + Chr(13);
cMsg:= cMsg + 'Size on disk : ' + IntToStr(RasterIO.InfoSizeDisk);
if (MessageBox(Handle, PChar(cMsg),'Write this file on disk ', MB_YESNO) = IDNO ) then
begin
GlobalFree (hMem);
Exit;
end;
lpMem:= GlobalLock(hMem);
if (lpMem = Nil) then
begin
GlobalFree (hMem);
ShowMessage('Error calling GlobalLock');
Exit;
end;
hf := FileCreate('d:\pictures\savemem.cmp');
if (hf = -1) then
begin
GlobalUnlock (hMem);
GlobalFree (hMem);
ShowMessage ('Error calling OpenFile');
Exit;
end;
if FileWrite(hf, lpMem^, iSize) <> iSize Then
MessageDlg ('Error calling FileWrite', mtError, [mbOK], 0)
else
MessageDlg ('The file was saved successfully on disk.', mtInformation, [mbOK], 0);
FileClose (hf);
GlobalUnlock (hMem);
GlobalFree (hMem);
end;