User Tools

Site Tools


saving_20a_20gif_20image

Saving a GIF image

by Richard Russell, December 2006

The main BBC BASIC for Windows documentation explains how to display a GIF image and elsewhere you can find out how to load a GIF image for use in your program. This article describes how to save an image as a GIF file. It relies on the presence of the GDI Plus library so will work only on Windows XP (or later) or if you have specifically installed GDIPLUS.DLL on the target computer. Microsoft permits you to redistribute this file so you can include it with your program if necessary.

To begin with you need to have a handle to the bitmap you want to save (see below if instead the bitmap is in the form of a DIB). One way of obtaining a handle is to load the image from a file (e.g. a BMP file):

        bmpfile$ = "\Windows\Soap Bubbles.bmp"
        SYS "LoadImage", 0, bmpfile$, 0, 0, 0, 16 TO hbitmap%
Here the image is loaded at its original size. You can alternatively scale the image to different dimensions:\\ \\ 
        bmpfile$ = "\Windows\Soap Bubbles.bmp"
        SYS "LoadImage", 0, bmpfile$, 0, dx%, dy%, 16 TO hbitmap%

Where dx% and dy% are the wanted width and height of the image respectively (the scaling quality is not particularly good so for best results you might prefer to scale the image using a third-party program).

There are a number of other ways in which you might obtain a bitmap handle, which are outside the scope of this article. You can easily obtain a handle to whatever is displayed in your program's output window:

        SYS "GetCurrentObject", @memhdc%, 7 TO hbitmap%

but this ordinarily returns the entire 1920 x 1440 bitmap which is probably not what you want. To save just a region of your program's output window the easiest way is probably to save it first as a BMP file (using *GSAVE) then load it using LoadImage as shown above.

Note that a GIF file has a maximum of 256 different colours, and if you use the above methods a colour palette consisting of a standard selection of colours will be used. The palette will not be optimised to the actual set of colours present in the source image. This can result in the quality of the GIF image being significantly worse than it needs to be.

If this is important you should first convert the image to an 8-bit (256-colour) BMP file, using an appropriate image editor capable of synthesising an optimised custom palette (for example using Heckbert quantisation). You should then load that BMP file as follows (using LoadImage will not preserve the palette):

        bmpfile$ = "tcf.bmp"
        bmpfile% = OPENIN(bmpfile$)
        IF bmpfile% = 0 ERROR 100, "Couldn't open file "+bmpfile$
        size% = EXT#bmpfile%
        CLOSE #bmpfile%
        DIM bmpfile% size%-1
        OSCLI "LOAD """+bmpfile$+""" "+STR$~bmpfile%
 
        SYS "CreateDIBSection", @memhdc%, bmpfile%+14, 0, ^vbits%, 0, 0 TO hbitmap%
        IF hbitmap% = 0 ERROR 100, "Couldn't create DIBSection"
        O% = bmpfile%!10
        FOR I% = 0 TO size%-O%-4 STEP 4
          vbits%!I% = bmpfile%!(I%+O%)
        NEXT

Once you've got a handle to the bitmap you simply save it as a GIF file as follows:

        PROCsavegif(hbitmap%, filename$)

Here filename$ is the name of the GIF file to create.

One you've saved the file you should delete the bitmap handle:

        SYS "DeleteObject", hbitmap%

Finally, here's the code for PROCsavegif itself:

        DEF PROCsavegif(hbitmap%, filename$)
        LOCAL gdiplus%, ole32%
 
        SYS "LoadLibrary", "GDIPLUS.DLL" TO gdiplus%
        IF gdiplus% = 0 ERROR 100, "Couldn't load GDIPLUS.DLL"
        SYS "GetProcAddress", gdiplus%, "GdiplusStartup" TO `GdiplusStartup`
        SYS "GetProcAddress", gdiplus%, "GdiplusShutdown" TO `GdiplusShutdown`
        SYS "GetProcAddress", gdiplus%, "GdipCreateBitmapFromHBITMAP" TO `GdipCreateBitmapFromHBITMAP`
        SYS "GetProcAddress", gdiplus%, "GdipDisposeImage" TO `GdipDisposeImage`
        SYS "GetProcAddress", gdiplus%, "GdipSaveImageToFile" TO `GdipSaveImageToFile`
        SYS "LoadLibrary", "OLE32.DLL" TO ole32%
        SYS "GetProcAddress", ole32%, "CLSIDFromString" TO `CLSIDFromString`
 
        LOCAL tSI{}, lRes%, lGDIP%, lBitmap%, tGifEncoder%, guid%, filename%
 
        DIM tGifEncoder% LOCAL 15, guid% LOCAL 79, filename% LOCAL 2*LEN(filename$)+1
        DIM tSI{GdiplusVersion%, DebugEventCallback%, \
        \       SuppressBackgroundThread%, SuppressExternalCodecs%}
 
        REM Initialize GDI+
        tSI.GdiplusVersion% = 1
        SYS `GdiplusStartup`, ^lGDIP%, tSI{}, 0 TO lRes%
        IF lRes% ERROR 100, "GDI+ error "+STR$(lRes%)
 
        REM Create the GDI+ bitmap from the image handle
        SYS `GdipCreateBitmapFromHBITMAP`, hbitmap%, 0, ^lBitmap% TO lRes%
        IF lRes% ERROR 100, "GDI+ error "+STR$(lRes%)
 
        REM Initialize the encoder GUID
        SYS "MultiByteToWideChar", 0, 0, "{557CF402-1A04-11D3-9A73-0000F81EF32E}", -1, guid%, 40
        SYS `CLSIDFromString`, guid%, tGifEncoder%
 
        REM Save the image
        SYS "MultiByteToWideChar", 0, 0, filename$, -1, filename%, LEN(filename$)+1
        SYS `GdipSaveImageToFile`, lBitmap%, filename%, tGifEncoder%, 0 TO lRes%
        IF lRes% ERROR 100, "GDI+ error "+STR$(lRes%)
 
        REM Destroy the bitmap
        SYS `GdipDisposeImage`, lBitmap%
 
        REM Shutdown GDI+
        SYS `GdiplusShutdown`, lGDIP%
        SYS "FreeLibrary", gdiplus%
 
        ENDPROC

If, instead of a bitmap handle, you have a bitmap (DIB) stored in memory you can use this alternative routine:

        DEF PROCsavegifdib(dib%, bmi%, filename$)
        LOCAL gdiplus%, ole32%
 
        SYS "LoadLibrary", "GDIPLUS.DLL" TO gdiplus%
        IF gdiplus% = 0 ERROR 100, "Couldn't load GDIPLUS.DLL"
        SYS "GetProcAddress", gdiplus%, "GdiplusStartup" TO `GdiplusStartup`
        SYS "GetProcAddress", gdiplus%, "GdiplusShutdown" TO `GdiplusShutdown`
        SYS "GetProcAddress", gdiplus%, "GdipCreateBitmapFromGdiDib" TO `GdipCreateBitmapFromGdiDib`
        SYS "GetProcAddress", gdiplus%, "GdipDisposeImage" TO `GdipDisposeImage`
        SYS "GetProcAddress", gdiplus%, "GdipSaveImageToFile" TO `GdipSaveImageToFile`
        SYS "LoadLibrary", "OLE32.DLL" TO ole32%
        SYS "GetProcAddress", ole32%, "CLSIDFromString" TO `CLSIDFromString`
 
        LOCAL tSI{}, lRes%, lGDIP%, lBitmap%, tGifEncoder%, guid%, filename%
 
        DIM tGifEncoder% LOCAL 15, guid% LOCAL 79, filename% LOCAL 2*LEN(filename$)+1
        DIM tSI{GdiplusVersion%, DebugEventCallback%, \
        \       SuppressBackgroundThread%, SuppressExternalCodecs%}
 
        REM Initialize GDI+
        tSI.GdiplusVersion% = 1
        SYS `GdiplusStartup`, ^lGDIP%, tSI{}, 0 TO lRes%
        IF lRes% ERROR 100, "GDI+ error "+STR$(lRes%)
 
        REM Create the GDI+ bitmap from the DIB
        SYS `GdipCreateBitmapFromGdiDib`, bmi%, dib%, ^lBitmap% TO lRes%
        IF lRes% ERROR 100, "GDI+ error "+STR$(lRes%)
 
        REM Initialize the encoder GUID
        SYS "MultiByteToWideChar", 0, 0, "{557CF402-1A04-11D3-9A73-0000F81EF32E}", -1, guid%, 40
        SYS `CLSIDFromString`, guid%, tGifEncoder%
 
        REM Save the image
        SYS "MultiByteToWideChar", 0, 0, filename$, -1, filename%, LEN(filename$)+1
        SYS `GdipSaveImageToFile`, lBitmap%, filename%, tGifEncoder%, 0 TO lRes%
        IF lRes% ERROR 100, "GDI+ error "+STR$(lRes%)
 
        REM Destroy the bitmap
        SYS `GdipDisposeImage`, lBitmap%
 
        REM Shutdown GDI+
        SYS `GdiplusShutdown`, lGDIP%
        SYS "FreeLibrary", gdiplus%
 
        ENDPROC

You would call it as follows:

        PROCsavegifdib(dibits%, bmi{}, filename$)

where dibits% is the address of the bitmap data and bmi{} is a BITMAPINFO structure containing the dimensions etc. and (optionally) colour palette for the bitmap.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
saving_20a_20gif_20image.txt · Last modified: 2024/01/05 00:21 by 127.0.0.1