Saves a bitmap to a file in a memory buffer that the user must allocate. The output can be in any of the supported compressed or uncompressed file formats.


#include "l_bitmap.h"

L_LTFIL_API L_INT L_SaveBitmapBuffer(pBuffer, uInitialBufferSize, puFinalFileSize, pBitmap, nFormat, nBitsPerPixel, nQFactor, pfnSaveBufferCB, pUserData, pSaveOptions)


L_UCHAR* pBuffer

Pointer to a buffer allocated by the user. Note that if this buffer is not large enough to accommodate the file in memory, the SAVEBUFFERCALLBACK is called to allow the user to reallocate the buffer.

L_SIZE_T uInitialBufferSize

Size of pBuffer in bytes. Note that the buffer size can be reallocated in the pfnSaveBufferCB callback.

L_SIZE_T* puFinalFileSize

Pointer to a variable that will contain the final size of the file in memory. This is not the final size of the allocated buffer, which may be larger than the memory file.


Pointer to the bitmap handle that describes the data to be saved. The bitmap handle can contain the actual data, but does not have to, since the callback function can supply the data.

However, the bitmap handle must contain values for the following fields: Width, Height, BitsPerPixel, BytesPerLine, nColors, ViewPerspective, Order, and DitheringMethod. (The BytesPerLine value must be a multiple of four.)

L_INT nFormat

Output file format. For valid values, refer to Files To Be Included With Your Application.

L_INT nBitsPerPixel

Resulting file's pixel depth. Note that not all bits per pixel are available to all file formats. For valid values, refer to Files To Be Included With Your Application. If nBitsPerPixel is 0, the file will be stored using the closest BitsPerPixel value supported by that format. For example, if a file format supports 1, 4, and 24 BitsPerPixel, and the pBitmap->BitsPerPixel is 5, the file will be stored as 24 bit. Likewise, if the pBitmap->BitsPerPixel is 2, the file will be stored as 4 bit.

L_INT nQFactor

This parameter is used when saving an image to file format that supports quality factor (QFactor). QFactor is a number that determines the degree of loss in the compression process.

For possible values, refer to Compression Quality Factors.


Optional callback function for reallocating the buffer if it is not large enough to accommodate the save. NULL can be passed for this parameter. However, if NULL is passed for this parameter and the buffer is not large enough, the function will fail.

The callback function must adhere to the syntax described in SAVEBUFFERCALLBACK Function.

L_VOID* pUserData

Void pointer that you can use to pass one or more additional parameters that the callback function needs.

To use this feature, assign a value to a variable or create a structure that contains as many fields as you need. Then, in this parameter, pass the address of the variable or structure, casting it to L_VOID *. The callback function, which receives the address in its own pUserData parameter, can cast it to a pointer of the appropriate data type to access your variable or structure.

If the additional parameters are not needed, you can pass NULL in this parameter.


Pointer to optional extended save options. Pass NULL to use the default save options.


Value Meaning
SUCCESS The function was successful.
< 1 An error occurred. Refer to Return Codes.


L_SaveFileBuffer saves a bitmap to a file in a memory buffer that the user must allocate. The output can be in any of the supported compressed or uncompressed file formats. This function is similar to L_SaveFileMemory, except the user is responsible for allocating/reallocating buffers. The SAVEBUFFERCALLBACK notifies the user if the buffer is not large enough. The user can then reallocate the buffer. For more information, see SAVEBUFFERCALLBACK.

If the user wants to allocate a buffer that is large enough to hold the file in memory, an upper bound on the file size can be estimated by the following formula:

RequiredBytes = (Image Width) * (Image Height) * (BitsPerPixel) / 8

Note that this is an estimate that does not include the size of the file header, and does not take any compression into account. It is best to use a SAVEBUFFERCALLBACK so that the user can be notified if the buffer is not large enough.

Support for 12 and 16-bit grayscale images is only available in the Document and Medical Imaging toolkits.

This function cannot be used in combination with L_RedirectIO.


This function supports signed data images, but only DICOM and TIFF formats support signed data. This function will return an error code if you attempt to save a signed image to a format other than DICOM or TIFF.

If the bitmap has a region, the region stored in the bitmap will be saved, if the image is saved as one of the TIFF file formats.

In LEADTOOLS version 17 and up, when saving a colored image (such as a 24-bits per pixel image) to bitonal (1-bit per pixel), the toolkit will not use any dithering when converting the image data. This is done because dithering is not the recommended when converting colored images containing text for document processing such as OCR and Barcode. The result text will be fuzzy and hard for a recognition engine to process. To save a colored image as bitonal with Floyd-Stein dithering (the behavior of LEADTOOLS 16.5 and earlier) use the ESO_USEDITHERINGMETHOD along with BITMAPHANDLE.DitheringMethod as illustrated below:

// 'pBitmap' is a colored BITMAPHANDLE 
// Setup FloydStein dithering: 
bitmapHandle.DitheringMethod = FLOYD_STEIN_DITHERING; 
SAVEFILEOPTION saveOptions = {0}; 
L_GetDefaultSaveFileOption(&saveOptions, sizeof(SAVEFILEOPTION)); 
// Save the bitmap as 1-bpp with auto-dithering: 
L_SaveBitmap(fileName, &bitmapHandle, FILE_CCITT_GROUP4, 1, 0, &saveOptions); 
// or any other L_SaveBitmapXyz or L_SaveFileXyz functions such as: 
// L_SaveFile(fileName, &bitmapHandle, FILE_CCITT_GROUP4, 1, 0, 0, NULL, NULL, &saveOptions) 

Required DLLs and Libraries


Win32, x64, Linux.

See Also




This example loads the file 'pszFile', and then saves it to a memory buffer
that the caller allocates. Note that when the buffer becomes filled,
the ExampleSaveBufferCB callback is called to notify the caller to reallocate the buffer.
After the save is completed, the size of the file in memory is displayed.
Finally, the memory buffer containing the file is loaded into pBitmap

typedef struct _tagMYBUFFER 
   L_UCHAR   *pBuffer; 
   L_SIZE_T  dwBufferSize; 
L_INT EXT_CALLBACK ExampleSaveBufferCB(L_SIZE_T    uRequiredSize, 
                                       L_UCHAR  ** ppBuffer, 
                                       L_SIZE_T *  pdwBufferSize, 
                                       L_VOID*     pUserData); 
L_INT SampleSaveBitmapBufferExample(L_TCHAR *pszFile, pBITMAPHANDLE pBitmap) 
   L_INT nRet; 
   L_SIZE_T dwFinalFileSize; 
   L_TCHAR szMsg[100]; 
   MYBUFFER MyBuffer; 
   MyBuffer.dwBufferSize = 100; 
   MyBuffer.dwBufferSize = 0; 
   MyBuffer.pBuffer = NULL; 
   nRet = L_LoadBitmap(pszFile, &Bitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGR, NULL, NULL); 
   if (nRet != SUCCESS) 
      return nRet; 
   MyBuffer.pBuffer = (L_UCHAR  *)malloc(MyBuffer.dwBufferSize);  //large enough to hold rgbw.bmp 
   if (MyBuffer.pBuffer == NULL) 
      return FAILURE; 
   nRet = L_SaveBitmapBuffer(MyBuffer.pBuffer, 
   if (nRet != SUCCESS) 
      return nRet; 
   wsprintf(szMsg, TEXT("Final Buffer Size: %d"), dwFinalFileSize); 
   MessageBox(NULL, szMsg, TEXT("Notice!"), MB_OK); 
   nRet = L_LoadBitmapMemory (MyBuffer.pBuffer, pBitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGR, dwFinalFileSize, NULL, NULL); 
   return SUCCESS; 

Help Version 22.0.2023.7.11
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Raster Imaging C API Help

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.