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


#include "l_bitmap.h"

L_LTFIL_API L_INT L_SaveFileMemory(hHandle, pBitmap, nFormat, nBitsPerPixel, nQFactor, uFlags, pFunction, lpUserData, uSize, pSaveOptions)


L_HANDLE* hHandle

Pointer to the memory handle that will be updated with a new memory handle that contains the saved image. To append a page to an existing image, this pointer should point to a valid memory handle (pHandle!=NULL). If you have no image saved in memory, this pointer should point to NULL (pHandle=NULL).

If you pass a valid memory handle, the old handle will become invalid and a new handle created. The new handle may have the same value as the old handle, however, this is not guaranteed.


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 your 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 closet 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

L_UINT uFlags

Binary flags that determine the behavior of L_SaveFile. You can specify one of the following values:

Value Meaning
SAVEFILE_FIXEDPALETTE [0x0001] The function uses the fixed palette for images that are saved as 8 bits per pixel or less.
SAVEFILE_OPTIMIZEDPALETTE [0x0002] The function uses the individual image's optimized palette for images that are saved as 8 bits per pixel or less. The optimized palette must be included in the bitmap handle.
SAVEFILE_MULTIPAGE [0x0004] The function saves the image in a multipage file. It appends the image as the last one in the file. You can save multipage images in PCX, GIF, and most TIFF file formats (including JTIF, but excluding EXIF).


Optional callback function for additional processing.

If you do not provide a callback function, use NULL as the value of this parameter.

If you do provide a callback function, use the function pointer as the value of this parameter.

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

L_VOID* lpUserData

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 lpUserData 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.

L_SIZE_T* uSize

Pointer to the size of the memory handle. If you are appending a page to an image in memory, and you specify a valid memory handle in *pHandle, uSize should contain the exact size of *pHandle. If you have no image in memory, this will initially be ignored. After returning from the function, the memory location pointed to by uSize will be updated with the size of the memory handle.


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.


To use this function, do the following:

  1. Declare a memory handle. You can then pass its address in this function, which will allocate the memory, save the bitmap, and unlock the memory. Set the value of the HANDLE to NULL.

  2. Declare a long integer (L_UINT32) variable for the file-size. You can then pass its address in this function, which will update its value with the size of the file.

  3. Call this function to save the bitmap in the specified memory handle.

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 if you attempt to save a signed image to a format other than DICOM or TIFF.

For information on saving bitmaps that have been window leveled, refer to Saving Window-Leveled Bitmaps.

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.

You can convert one file format to another when you save a file. You can also use the high-level function, L_FileConvert, to convert any possible format to any other possible format.

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




L_INT SaveFileMemoryExample(L_TCHAR     * pFilename, 
                                           pBITMAPHANDLE pBitmapHandle, 
                                           L_INT         nType, 
                                           HWND          hMainWnd) 
   L_INT nRet; 
   HANDLE  hFile=NULL; 
   HANDLE hData=NULL; 
   L_CHAR  *pData; 
   L_CHAR  *pDataMover; 
   L_SIZE_T uSize; 
   SAVEFILEOPTION SaveFileOption; 
   LOADFILEOPTION LoadFileOption; 
   DWORD dwSizeWrite; 
   L_GetDefaultSaveFileOption(&SaveFileOption, sizeof(SAVEFILEOPTION)); 
   L_GetDefaultLoadFileOption(&LoadFileOption, sizeof(LOADFILEOPTION)); 
   SaveFileOption.PageNumber = 1; 
   /* save first page */ 
   nRet = L_SaveFileMemory (&hData, pBitmapHandle, FILE_TIF, 0, 0, 0, NULL, NULL, &uSize, &SaveFileOption); 
   if(nRet != SUCCESS) 
      return nRet; 
   /* flip image */ 
   nRet = L_FlipBitmap(pBitmapHandle); 
   if(nRet != SUCCESS) 
      return nRet; 
   SaveFileOption.PageNumber = 2; 
   /* save second page */ 
   nRet = L_SaveFileMemory (&hData, pBitmapHandle, FILE_TIF, 0, 0, 0, NULL, NULL, &uSize, &SaveFileOption); 
   if(nRet != SUCCESS) 
      return nRet; 
   pData = (L_CHAR  *) GlobalLock (hData); 
   pDataMover = pData; 
   MessageBox (NULL, TEXT("Image Saved To Memory!"), TEXT("SUCCESS"), MB_OK); 
   L_FreeBitmap (pBitmapHandle); 
   InvalidateRect (hMainWnd, NULL, TRUE); 
   UpdateWindow (hMainWnd); 
   /* Open/Create the file to write. */ 
   if (hFile == INVALID_HANDLE_VALUE) 
      MessageBox (NULL, TEXT("Error Opening File!"), TEXT("Error"), MB_OK); 
      return (FALSE); /* Failure on creation! */ 
   /* Write the file in a loop, because the file could be larger than a single write can request. */ 
   while (uSize > 0) 
      nRet = WriteFile(hFile, pDataMover, 
                      (L_INT) min (uSize, (L_UINT32) 32000), &dwSizeWrite, NULL); 
      if (!nRet) 
         MessageBox (NULL, TEXT("Error writing file!"), TEXT("Error"), MB_OK); 
         nRet = FALSE;          /* Failure on write! */ 
         uSize -= dwSizeWrite; 
         pDataMover += dwSizeWrite; 
   if (nRet) 
      MessageBox (NULL, TEXT("Memory File Has Been Written To Disk!"), TEXT("SUCCESS"), MB_OK); 
   GlobalUnlock (hData); 
   GlobalFree (hData); 
   /* load page 2 from the saved file */ 
   LoadFileOption.PageNumber = 2; 
   nRet = L_LoadBitmap (pFilename, pBitmapHandle, sizeof(BITMAPHANDLE), 0, ORDER_BGR, &LoadFileOption, NULL); 
   if(nRet == SUCCESS) 
      InvalidateRect (hMainWnd, NULL, TRUE); 
      UpdateWindow (hMainWnd); 
      MessageBox (NULL, TEXT("Error Loading from file!"), TEXT("Error"), MB_OK); 
      return nRet; 
   return SUCCESS; 
Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 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.