[comp.windows.ms] Saving Bitmaps

kipnis@janus.berkeley.edu (Gary Kipnis) (08/04/89)

   Hi, I am working on a program that uses a big color bitmap, I would like
   to save this bitmap when my application is iconified so that I can free
   up the memory, how would I go about dumping a bitmap to a disk and then
   reading it from a disk

   Thanks in advance...

   gary

patrickd@chinet.chi.il.us (Patrick Deupree) (08/04/89)

In article <30368@ucbvax.BERKELEY.EDU> kipnis@janus.berkeley.edu (Gary Kipnis) writes:
>
>   Hi, I am working on a program that uses a big color bitmap, I would like
>   to save this bitmap when my application is iconified so that I can free
>   up the memory, how would I go about dumping a bitmap to a disk and then
>   reading it from a disk

Since all a bitmap is is a structure that contains fields of header information
plus an array of binary data (in the case of my program in Actor I used a
Struct) it's really pretty easy.  All you'd have to do is create a file and
store your header info first, then store the binary data.  Doing this is just 
a matter of normal file IO routines.

-- 
"I place my faith in fools.  Self confidence, my friends call it."
					-Edgar Allen Poe

Patrick Deupree -> patrickd@chinet.chi.il.us

bturner@hpcvlx.HP.COM (Bill Turner) (08/04/89)

>   Hi, I am working on a program that uses a big color bitmap, I would like
>   to save this bitmap when my application is iconified so that I can free
>   up the memory, how would I go about dumping a bitmap to a disk and then
>   reading it from a disk

Create a compatable DC for the display, and a compatible bitmap (NOTE! large
color bitmaps take lots of memory, so you may want to do this in "bands",
slicing the full image into smaller pieces).  BitBlt from the display to the
memory DC, then use GetBitmapBits to get a memory copy of the bitmap, which
can be written to a file.  To restore the picture, read the file into memory,
use SetBitmapBits to copy them into a memory bitmap, then BitBlt to the display.

    BYTE buffer[];

    hMemDC = CreateCompatibleDC(hDC);
    hMemBits = CreateCompatibleBitmap(hDC, nWidth, nHeight);  /* Note below */
    SelectObject(hMemDC, hMemBits);
    BitBlt(hMemDC, xDest, yDest, nWidth, nHeight, hDC, xSrc, ySrc, SRCCOPY);
	/* Note below */
    dSize = GetBitmapBits(hMemBits, sizeof(buffer), buffer);
	/* Now you have the bits in buffer */
    DeleteDC(hMemDC);
    DeleteObject(hMemBits);

NOTES:
    CreateCompatibleBitmap will create a bitmap compatible with a device iff
a device DC is used.  If you had done CreateCompatibleBitmap(hMemDC,...), then
the bitmap would have been monochrome.

    In the BitBlt, xDest and yDest will probably be (0, 0).

    If you will be banding, repeat the BitBlt/GetBitmapBits as required.

CAVEAT:
    The format of color bitmaps isn't as standard as that for monochrome.  If
you save an EGA color bitmap, don't expect to be able to restore it onto a
CGA display.

--Bill Turner (bturner@hp-pcd.hp.com)
HP Corvallis Information Systems