如题,数组为字节型,每元素大小为灰度值。关键是调色版的创建与填充
void CCustemCtrl3Dlg::OnButton2()
{
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
// TODO: Add your control notification handler code here
int i,j;
BYTE* m_bitData=new BYTE[800*600];
ZeroMemory((void*)m_bitData, 800*600);
for (i=0; i<600; i++)
{
for(j=0; j<800; j++)
{
m_bitData[i*800+j]=255*i/600;
}
}
LONG lWidth=800;
LONG lHeight=600;
LPBITMAPINFO lpbmi;
// BMP文件头的句柄
HANDLE hBmpFileHead;
//计算信息头和调色板的大小
hBmpFileHead=::GlobalAlloc(GHND,sizeof(BITMAPINFOHEADER)
+ sizeof(RGBQUAD)
* 256);
if(hBmpFileHead==0)
{
AfxMessageBox("分配内存失败");
return;
}
//填充信息头
lpbmi=(LPBITMAPINFO)::GlobalLock((HGLOBAL)hBmpFileHead);
lpbmi->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
lpbmi->bmiHeader.biWidth=lWidth;
lpbmi->bmiHeader.biHeight=lHeight;
lpbmi->bmiHeader.biPlanes=1;
lpbmi->bmiHeader.biBitCount=8;
lpbmi->bmiHeader.biCompression=BI_RGB;
lpbmi->bmiHeader.biSizeImage=WIDTHBYTES(lWidth*8)*lHeight;
lpbmi->bmiHeader.biXPelsPerMeter=0;
lpbmi->bmiHeader.biYPelsPerMeter=0;
lpbmi->bmiHeader.biClrUsed=256;
lpbmi->bmiHeader.biClrImportant=256;
//填充256级灰度调色板
//int i,j;
for( i=0;i<256;i++)
{
lpbmi->bmiColors[i].rgbBlue=i;
lpbmi->bmiColors[i].rgbGreen=i;
lpbmi->bmiColors[i].rgbRed=i;
lpbmi->bmiColors[i].rgbReserved=0;
}
//填充文件头
BITMAPFILEHEADER bmfHeader;
ZeroMemory(&bmfHeader,sizeof(bmfHeader));
bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ sizeof(RGBQUAD) * 256;
bmfHeader.bfSize=lpbmi->bmiHeader.biSizeImage+bmfHeader.bfOffBits;
bmfHeader.bfType='MB';
BYTE* pBits=new BYTE[lpbmi->bmiHeader.biSizeImage];
ZeroMemory((void*)pBits, lpbmi->bmiHeader.biSizeImage);
for (i=0; i
for (j=0; j
pBits[WIDTHBYTES(lWidth*8)*(lHeight-1-i)+j]=m_bitData[i*lWidth+j];
}
}
CString str="c://test1.bmp";
HANDLE hFile=CreateFile(str,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
DWORD dwRet=0;
WriteFile(hFile,&bmfHeader,sizeof(bmfHeader),&dwRet,NULL);
WriteFile(hFile,&lpbmi->bmiHeader,sizeof(lpbmi->bmiHeader),&dwRet,NULL);
WriteFile(hFile,&lpbmi->bmiColors[0],sizeof(RGBQUAD) * 256,&dwRet,NULL);
WriteFile(hFile,pBits,lpbmi->bmiHeader.biSizeImage,&dwRet,NULL);
CloseHandle(hFile);
}
delete[] pBits;
delete[] m_bitData;
::GlobalUnlock((HGLOBAL)hBmpFileHead);
::GlobalFree((HGLOBAL) hBmpFileHead);
}