TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: KURT KUZBA
from: ALBERTO MONTEIRO
date: 1997-11-30 19:31:00
subject: Save Bitmap in M$C

AM>   I would like to know if there is any function (or
AM>   whatever) that saves a bitmap to a file.
AM>   I now have a working routine. Do you want to see it?
KK>   Yes! One more wheel I won't need to re-invent. :)
It's from the labirinthic helps of M$C; the original routines are
handle-oriented; I guess they can be adapted to use in MFC, but
I didn't dare to do it:
#include "stdafx.h"
void errhandler(char *s, HWND hwnd)
{
  MessageBox(hwnd, s, "erro", MB_OK);
}
#if !defined(MAXWRITE)
#define MAXWRITE 666666  // don't know what is this,
                         //so I put a demoniac number #endif
/*
Many applications store images permanently as files. For example,
drawing applications store pictures, spreadsheet applications store
charts, CAD applications store drawings, and so on.
If you are writing an application that will store a bitmapped image
in a file, you should use the Windows file format described in Bitmap
Storage. In order to store a bitmap in this format, you must
initialize a BITMAPINFO structure (consisting of a BITMAPFILEHEADER
structure and an array of RGBQUAD structures), as well as an array of
palette indices.
The following example code defines a function that allocates memory
for and initializes members within a BITMAPINFOHEADER structure.
*/
PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp)
{
    BITMAP bmp;
    PBITMAPINFO pbmi;
    WORD    cClrBits;
    /* Retrieve the bitmap's color format, width, and height. */
    if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
        errhandler("GetObject", hwnd);
    /* Convert the color format to a count of bits. */
    cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
    if (cClrBits == 1)
        cClrBits = 1;
    else if (cClrBits <= 4)
        cClrBits = 4;
    else if (cClrBits <= 8)
        cClrBits = 8;
    else if (cClrBits <= 16)
        cClrBits = 16;
    else if (cClrBits <= 24)
        cClrBits = 24;
    else
        cClrBits = 32;
    /*
     * Allocate memory for the BITMAPINFO structure. (This structure
     * contains a BITMAPINFOHEADER structure and an array of RGBQUAD data
     * structures.)
     */
    if (cClrBits != 24)
         pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
                    sizeof(BITMAPINFOHEADER) +
                    sizeof(RGBQUAD) * (2^cClrBits));
    /*
     * There is no RGBQUAD array for the 24-bit-per-pixel format.
     */
    else
         pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
                    sizeof(BITMAPINFOHEADER));
    /* Initialize the fields in the BITMAPINFO structure. */
    pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    pbmi->bmiHeader.biWidth = bmp.bmWidth;
    pbmi->bmiHeader.biHeight = bmp.bmHeight;
    pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
    pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
    if (cClrBits < 24)
        pbmi->bmiHeader.biClrUsed = 2^cClrBits;
    /* If the bitmap is not compressed, set the BI_RGB flag. */
    pbmi->bmiHeader.biCompression = BI_RGB;
    /*
     * Compute the number of bytes in the array of color
     * indices and store the result in biSizeImage.
     */
    pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) /8
                                  * pbmi->bmiHeader.biHeight
                                  * cClrBits;
    /*
     * Set biClrImportant to 0, indicating that all of the
     * device colors are important.
     */
    pbmi->bmiHeader.biClrImportant = 0;
    return pbmi;
}
/*
The following example code defines a function that initializes the
remaining structures, retrieves the array of palette indices, opens
the file, copies the data, and closes the file.
*/
void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi,
                  HBITMAP hBMP, HDC hDC);
// It comes in next message, to limit messages in 150 lines
---
 þ SLMR 2.1a þ Simple smiles elude psychotic eyes (Slayer-Dead Skin Mask
--- FMail/386 1.02
---------------
* Origin: CentroIn! +55-21-205-0281, 41 lines, 24h, RJ/Brazil (4:802/21)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.