freeimage.h

FreeImage_SaveToMemory

DLL_API BOOL DLL_CALLCONV FreeImage_SaveToMemory(FREE_IMAGE_FORMAT fif, FIBITMAP *dib,
FIMEMORY *stream, int flags FI_DEFAULT(0));

Esta función hace con canales de memoria lo que FreeImage_Save hace con ficheros. FreeImage_SaveToMemory guarda un FBITMAP previamente cargado a un fichero en ememoria gestionado por FreeImage. El primer parámetro define el tipo de mapa de bits a guardar. Por ejemplo, si se pasa FIF_BMP, se guarda un fichero BMP (una descripción de las constantes posibles FREE_IMAGE_FORMAT está disponible en la tabla 1). El segundo parámetro es el canal de memoria donde se debe guardar el mapa de bits. Cuando el puntero de fichero en memoria apunta al principio del fichero, los datos existentes son sobrescritos. En caso contrario se pueden guardar múltiples imágenes en el mismo canal.

Hay que tener en cuenta que algunos plugins de mapas de bits tienen restricciones sobre los tipos de mapas de bits que pueden salvar. Por ejemplo, el plugin JPEG sólo puede guardar mapas de bits en escala de grises de 24-bit y 8-bit. El último parámetro se usa para modificar el comportamiento o activar alguna característica del plugin de mapa de bits. Cada plugin tiene su propio conjunto de parámetros.

Algunas funciones de guardado de mapas de bits pueden recibir parámetros para modificar el comportamiento (ver tabla 4). Cuando el parámetro no está disponible o no se use se puede pasar el valor 0 o <TYPE_OF_BITMAP>_DEFAULT (por ejemplo BMP_DEFAULT, ICO_DEFAULT, etc).

void testSaveMemIO(const char *lpszPathName) {
    FIMEMORY *hmem = NULL;
    // load and decode a regular file
    FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
    FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, 0);

    // open a memory stream
    hmem = FreeImage_OpenMemory();
    // encode and save the image to the memory
    FreeImage_SaveToMemory(fif, dib, hmem, 0);
    // at this point, hmem contains the entire data in memory stored in fif format.
    // the amount of space used by the memory is equal to file_size
    FreeImage_SeekMemory(hmem, 0, SEEK_END);
    long file_size = FreeImage_TellMemory(hmem);
    printf("File size : %ld\n", file_size);
    // its easy to load an image from memory as well
    // seek to the start of the memory stream
    FreeImage_SeekMemory(hmem, 0L, SEEK_SET);

    // get the file type
    FREE_IMAGE_FORMAT mem_fif = FreeImage_GetFileTypeFromMemory(hmem, 0);

    // load an image from the memory handle
    FIBITMAP *check = FreeImage_LoadFromMemory(mem_fif, hmem, 0);
    // save as a regular file
    FreeImage_Save(FIF_PNG, check, "dump.png", PNG_DEFAULT);
    // make sure to close the stream since FreeImage_SaveToMemory
    // will cause internal memory allocations and this is the only
    // way to free this allocated memory
    FreeImage_CloseMemory(hmem);
    FreeImage_Unload(check);
    FreeImage_Unload(dib);
}