SaveBuffer example for Delphi
var
pBuffer: Pointer;
uImageSize: Cardinal;
uFinalBufSize: Cardinal;
nRet: Integer;
hf: integer;
begin
// Loading an image
LEADImage1.Load ('v:\images\image1.cmp', 0, 1, 1);
// Calculate the Image Size so we can allocate enough Buffer to hold it.
uImageSize:= Round((LEADImage1.BitmapWidth *LEADImage1.BitmapHeight * 24)/8);
// Allocate a buffer to hold the Bitmap
pBuffer:= AllocMem (uImageSize) ;
if ( pBuffer = Nil ) then
begin
ShowMessage ('Not enough Memory');
Exit;
end;
// Saving the Bitmap in Memory
nRet := LEADImage1.SaveBuffer (pBuffer, uImageSize, FILE_CMP, 24, 2, @uFinalBufSize );
if ( nRet = SUCCESS ) then
begin
// Create a file on Disk
hf := FileCreate('c:\temp\SaveBuf.cmp');
if (hf = -1) Then
begin
FreeMem (pBuffer);
MessageDlg ('Can not create file', mtError, [mbOK], 0);
Exit;
end;
// Writing the File in Memory to Disk
if (FileWrite(hf, pBuffer^, uFinalBufSize) <> Integer(uFinalBufSize) ) then
MessageDlg ('Error calling FileWrite', mtError, [mbOK], 0)
else
begin
MessageDlg ('The file was saved successfully on disk.', mtInformation, [mbOK], 0);
nRet:= SUCCESS;
end;
FileClose (hf);
if ( pBuffer <> Nil ) then
FreeMem (pBuffer);
// Loading the Saved Bitmap from Disk
if ( nRet = SUCCESS ) then
LEADImage2.Load ('c:\temp\SaveBuf.cmp', 0, 1, 1 ) ;
Exit;
end;
// Free the Buffer
if ( pBuffer <> Nil ) then
FreeMem (pBuffer);
end;