#include void Write32(HANDLE fh,const DWORD *lpPixel,int w,int h) { int extra; if(w*3%4) extra=4-w*3%4; else extra=0; DWORD dwWriteSize; int zero=0; for(int y=0;ybmiHeader,sizeof(BITMAPINFOHEADER)); int bitCount=bmpInfoH.biBitCount; if(bitCount!=32 && bitCount!=24 && bitCount!=8){ char str[8]; wsprintf(str,"%d",bitCount); MessageBox(NULL,"対応していないビット数です",str,MB_OK); return -1; } if(bitCount==32) bmpInfoH.biBitCount=24; int w=bmpInfoH.biWidth , h=bmpInfoH.biHeight; DWORD nColorTable=bmpInfoH.biClrUsed; if(bitCount==8 && nColorTable==0) nColorTable=256; int len; if(w*(bitCount/8)%4) len=w*(bitCount/8)+(4-w*(bitCount/8)%4); else len=w*(bitCount/8); BITMAPFILEHEADER bmpfh; bmpfh.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+len*h; bmpfh.bfType=('M'<<8)+'B'; bmpfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); if(bitCount==8){ bmpfh.bfSize+=nColorTable*4; bmpfh.bfOffBits+=nColorTable*4; } HANDLE fh=CreateFile(lpFileName,GENERIC_WRITE,0,NULL, CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL); if(fh==INVALID_HANDLE_VALUE){ MessageBox(NULL,"同名のファイルが既に存在しています",lpFileName,MB_OK); return -2; } DWORD dwWriteSize; WriteFile(fh,&bmpfh,sizeof(BITMAPFILEHEADER),&dwWriteSize,NULL); WriteFile(fh,&bmpInfoH,sizeof(BITMAPINFOHEADER),&dwWriteSize,NULL); if(bitCount==8) WriteFile(fh,lpBmpInfo->bmiColors,nColorTable*4,&dwWriteSize,NULL); if(bitCount==32) Write32(fh,(LPDWORD)lpPixel,w,h); else WriteFile(fh,lpPixel,len*h,&dwWriteSize,NULL); CloseHandle(fh); return 0; } int SaveDDB(char *lpFileName,HDC hMemDC,int width,int height) { BITMAPINFO bmpInfo; LPBYTE lpPixel; //DIBの情報を設定する bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); bmpInfo.bmiHeader.biWidth=width; bmpInfo.bmiHeader.biHeight=height; bmpInfo.bmiHeader.biPlanes=1; bmpInfo.bmiHeader.biBitCount=24; bmpInfo.bmiHeader.biCompression=BI_RGB; //DIBSection作成 HBITMAP hBmp=CreateDIBSection(NULL,&bmpInfo,DIB_RGB_COLORS,(void**)&lpPixel,NULL,0); HDC hdcTemp=CreateCompatibleDC(NULL); SelectObject(hdcTemp,hBmp); BitBlt(hdcTemp,0,0,width,height,hMemDC,0,0,SRCCOPY); int ret=SaveDIB(lpFileName,lpPixel,&bmpInfo); DeleteDC(hdcTemp); DeleteObject(hBmp); return ret; }