#include "l_bitmap.h"

L_LTFIL_API L_INT L_SaveFile(pszFile, pBitmap, nFormat, nBitsPerPixel, nQFactor, uFlags, pfnCallback, pUserData, pSaveOptions)

L_TCHAR* pszFile;

name of the image file to save


pointer to the bitmap handle

L_INT nFormat;

output file format

L_INT nBitsPerPixel;

resulting file's pixel depth

L_INT nQFactor;

quality factor

L_UINT uFlags;

flags that determine function behavior


optional callback function

L_VOID* pUserData;

pointer to more parameters for the callback


pointer to optional extended save options

Creates a file in any of the supported compressed or uncompressed formats, using input data that your callback function supplies.

Parameter Description
pszFile Character string containing the name of the image file to save.
pBitmap 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.)
nFormat Output file format. For valid values, refer to Files To Be Included With Your Application.
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.
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.
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 multi-page file. It appends the image as the last one in the file. You can save multi-page images in PCX, GIF, and most TIFF file formats (including JTIF, but excluding EXIF).
  SAVEFILE_GRAYOUTPUT [0x0008] The function saves the image as grayscale.
pfnCallback 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.
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.
pSaveOptions Pointer to optional extended save options. Pass NULL to use the default save options.



The function was successful.

< 1

An error occurred. Refer to Return Codes.


If you provide a callback function, it must supply the image data. It can also do other useful things, such as updating a status bar.

If you do not provide a callback function, this function saves data from the specified bitmap.

If you use a callback and save the bitmap as a MacPaint (MAC) file, the image to be saved must have a width of 576 and a height of 720 (the required dimensions of that format).

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


More options are available in the SAVEFILEOPTION structure.


More options are available in the SAVEFILEOPTION structure.


If SAVE_MULTIPAGE is passed in uFlags, this takes precedence over the ESO_REPLACEPAGE or ESO_INSERTPAGE flags set in SAVEFILEOPTION.

Note: For information about loading and saving large TIFF files faster, refer to Loading and Saving Large TIFF/BigTIFF Files.

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 
// Set up 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 function such as: 
// L_SaveFile(fileName, &bitmapHandle, FILE_CCITT_GROUP4, 1, 0, 0, NULL, NULL, &saveOptions) 

NITF files contain both raster and non-raster data and so NITF files cannot be saved using the standard raster library. Use the separate NITF library to create and save NITF files.

Required DLLs and Libraries

File format DLLs

For a listing of the exact DLLs and Libraries needed, based on the toolkit version, refer to Files To Be Included With Your Application.


Win32, x64, Linux.

See Also


L_SaveBitmap, L_SaveFileOffset, L_FileConvert, L_SetComment, L_DlgSave, L_SaveFileMemory


Raster Image Functions: Saving Files


Raster Image Functions: Maintaining File Comments and Tags


Raster Image Functions: Low-Level Compression Functions


Raster Image Functions: Loading and Saving Stamp Images


Raster Image Functions: Redirecting Input and Output


Raster Image Functions: Input and Output


Loading and Saving Images

For a list of functions that utilize the LOADFILEOPTION or SAVEFILEOPTION structures, refer to Functions Utilizing the LOADFILEOPTION or SAVEFILEOPTION structures.


For complete sample code, refer to the SAVECB example. This example uses L_SaveFile with a callback function that supplies the image data. Refer to the FILESAVECALLBACK function to see how the callback function supplies the image data.

#define MAKE_IMAGE_PATH(pFileName) TEXT("C:\\Users\\Public\\Documents\\LEADTOOLS Images\\")pFileName 
L_UCHAR     * pBuffer, 
L_UINT        nRowBegin, 
L_UINT        nRowsToGet, 
L_VOID       * pUserData); 
L_INT SaveFileExample(HINSTANCE hInst,   /* Current instance of the application, set by the InitInstance function */ 
   L_INT nRet;            /* Return value                     */ 
   L_TCHAR szMessage[80]; /* Buffer for the MessageBox string */ 
   /* Set the dithering method for the save */ 
   LeadBitmap->DitheringMethod = FLOYD_STEIN_DITHERING; 
   /* Save the file as an 8-bit Windows BMP file with a fixed palette */ 
   /* Set the callback function for the L_SaveFile function.*/ 
   nRet = L_SaveFile(MAKE_IMAGE_PATH(TEXT("TEST2.BMP")), 
   FILE_BMP, 8, 0, 
   /* Notify the user with a message box */ 
   if (nRet == SUCCESS) 
      MessageBox (NULL, TEXT("File was saved"), TEXT("Notice"), MB_OK); 
      wsprintf (szMessage, TEXT("Error %d saving the file"), nRet); 
      MessageBox (NULL, szMessage, TEXT("Error"), MB_OK); 
      return nRet; 
   return SUCCESS; 

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Raster Imaging C API Help