DDBをBMPファイルに保存する事はできません。
読む込む関数はあるのに不便ですね。
従って、DIBとして扱う事でBMPファイルに保存していきます。
■DIBSectionをBMPファイルに保存する
前述のように、DDBをBMPファイルに保存する事はできないので、
DIBとして扱う事でBMPファイルに保存していきます。
って事は、前節の関数がそのまま使える?
そうです、前節の関数をそのまま使ってDIBSectionをBMPファイルに保存します。
■関数の呼び出し
32ビットDIBSectionを作成して、24ビットBMPファイルとして保存してみます。
#include<windows.h>
#include"SaveDIB.h"
#define WIDTH 197
#define HEIGHT 100
LRESULT CALLBACK WindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static BITMAPINFO bmpInfo;
static LPDWORD lpPixel;
static HBITMAP hBitmap;
static HDC hMemDC;
int x,y;
switch(uMsg) {
case WM_CREATE:
//DIBの情報を設定する
bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth=WIDTH;
bmpInfo.bmiHeader.biHeight=HEIGHT;
bmpInfo.bmiHeader.biPlanes=1;
bmpInfo.bmiHeader.biBitCount=32;
bmpInfo.bmiHeader.biCompression=BI_RGB;
//DIBSection作成
hdc=GetDC(hWnd);
hBitmap=CreateDIBSection(hdc,&bmpInfo,DIB_RGB_COLORS,(void**)&lpPixel,NULL,0);
hMemDC=CreateCompatibleDC(hdc);
SelectObject(hMemDC,hBitmap);
ReleaseDC(hWnd,hdc);
//描画
for(y=25;y<50;y++){
for(x=25;x<50;x++){
lpPixel[x+y*WIDTH]=0x00ffff00; //黄
}
}
//BMPファイルに保存
SaveDIB("DIBSection32_bmp24.bmp",(LPBYTE)lpPixel,&bmpInfo);
return 0;
case WM_DESTROY:
//自らlpPixelを解放するべからず
DeleteDC(hMemDC);
DeleteObject(hBitmap); //BMPを削除した時、lpPixelも自動的に解放される
PostQuitMessage(0);
return 0;
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
//表画面へ転送
BitBlt(hdc,0,0,WIDTH,HEIGHT,hMemDC,0,0,SRCCOPY);
EndPaint(hWnd,&ps);
return 0;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

★☆ ダウンロード ☆★
次に、24ビットDIBSectionを作成して、24ビットBMPファイルとして保存してみます。
#include<windows.h>
#include"SaveDIB.h"
#define WIDTH 197
#define HEIGHT 100
#define BITCOUNT 24
LRESULT CALLBACK WindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static BITMAPINFO bmpInfo;
static LPBYTE lpPixel;
static HBITMAP hBitmap;
static HDC hMemDC;
static int length;
int x,y;
switch(uMsg) {
case WM_CREATE:
//DIBの情報を設定する
bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth=WIDTH;
bmpInfo.bmiHeader.biHeight=HEIGHT;
bmpInfo.bmiHeader.biPlanes=1;
bmpInfo.bmiHeader.biBitCount=BITCOUNT;
bmpInfo.bmiHeader.biCompression=BI_RGB;
//DIBSection作成
hdc=GetDC(hWnd);
hBitmap=CreateDIBSection(hdc,&bmpInfo,DIB_RGB_COLORS,(void**)&lpPixel,NULL,0);
hMemDC=CreateCompatibleDC(hdc);
SelectObject(hMemDC,hBitmap);
ReleaseDC(hWnd,hdc);
//4の倍数に補正
if(WIDTH*(BITCOUNT/8)%4) length=WIDTH*(BITCOUNT/8)+(4-WIDTH*(BITCOUNT/8)%4);
else length=WIDTH*(BITCOUNT/8);
//描画
for(y=25;y<50;y++){
for(x=25;x<50;x++){
lpPixel[(x*3 )+y*length]=255; //B
lpPixel[(x*3+1)+y*length]=255; //G
lpPixel[(x*3+2)+y*length]=0; //R
}
}
//BMPファイルに保存
SaveDIB("DIBSection24_bmp24.bmp",lpPixel,&bmpInfo);
return 0;
case WM_DESTROY:
//自らlpPixelを解放するべからず
DeleteDC(hMemDC);
DeleteObject(hBitmap); //BMPを削除した時、lpPixelも自動的に解放される
PostQuitMessage(0);
return 0;
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
//表画面へ転送
BitBlt(hdc,0,0,WIDTH,HEIGHT,hMemDC,0,0,SRCCOPY);
EndPaint(hWnd,&ps);
return 0;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

★☆ ソースファイルの表示 ☆★
8ビットDIBSectionは作れません。
SaveDIB関数については前節をご覧下さい。