从其他程序中取得了位图资源(不是bmp文件,而是比如说一个exe文件里包含了位图资源),并取得了BITMAPINFOHEADER,现在想直接通过BITMAPINFOHEADER把那个位图显示在对话框的一个矩形区域里,看了MSDN说使用CreateDIBitmap()然后再StretchBlt(),但现在不会设置CreateDIBitmap函数里的第2、4、5个参数,请高手指点一下.HBITMAP CreateDIBitmap(
HDC hdc, // handle to DC
CONST BITMAPINFOHEADER *lpbmih, // bitmap data
DWORD fdwInit, // initialization option
CONST VOID *lpbInit, // initialization data
CONST BITMAPINFO *lpbmi, // color-format data
UINT fuUsage // color-data usage
)
HDC hdc, // handle to DC
CONST BITMAPINFOHEADER *lpbmih, // bitmap data
DWORD fdwInit, // initialization option
CONST VOID *lpbInit, // initialization data
CONST BITMAPINFO *lpbmi, // color-format data
UINT fuUsage // color-data usage
)
BOOL CreateBMPFile(LPBITMAPINFOHEADER pbih,CString pszFile)
{HANDLE hf; /* file handle */
BITMAPFILEHEADER hdr; /* bitmap file-header */
LPBYTE lpBits; /* memory pointer */
DWORD dwTotal; /* total count of bytes */
DWORD cb; /* incremental count of bytes */
BYTE *hp; /* byte pointer */
DWORD dwTmp;
DWORD MAXWRITE = 65535;
int iNumColors;
if (pbih->biBitCount <= 8)
iNumColors = (1 << pbih->biBitCount);
else
iNumColors = 0;
lpBits = (LPBYTE)pbih + pbih->biSize + (iNumColors * sizeof(RGBQUAD));
/* Create the .BMP file. */
hf = CreateFile(pszFile,
GENERIC_READ | GENERIC_WRITE,
(DWORD) 0,
(LPSECURITY_ATTRIBUTES) NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,
(HANDLE) NULL);
if (hf == INVALID_HANDLE_VALUE)
{
AfxMessageBox("Error Creating File");
return FALSE;
}
hdr.bfType = 0x4d42; /* 0x42 = "B" 0x4d = "M" */
/* Compute the size of the entire file. */ hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + pbih->biSize + iNumColors * sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
/* Compute the offset to the array of color indices. */
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + pbih->biSize + iNumColors* sizeof (RGBQUAD);
/* Copy the BITMAPFILEHEADER into the .BMP file. */
if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),
(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL))
{
MessageBox("写入文件时出错!",NULL,MB_ICONERROR|MB_OK);
return FALSE;
}
/* Copy the BITMAPINFOHEADER and RGBQUAD array into the file. */
if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)
+ iNumColors * sizeof (RGBQUAD),
(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL))
{
MessageBox("写入文件时出错!",NULL,MB_ICONERROR|MB_OK);
return FALSE;
}
/* Copy the array of color indices into the .BMP file. */
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
while (cb > MAXWRITE) {
if (!WriteFile(hf, (LPSTR) hp, (int) MAXWRITE,
(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL))
{
MessageBox("写入文件时出错!",NULL,MB_ICONERROR|MB_OK);
return FALSE;
}
cb-= MAXWRITE;
hp += MAXWRITE;
}
if (!WriteFile(hf, (LPSTR) hp, (int) cb,
(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL))
{
MessageBox("写入文件时出错!",NULL,MB_ICONERROR|MB_OK);
return FALSE;
}
/* Close the .BMP file. */
if (!CloseHandle(hf))
{
MessageBox("关闭文件时出错!",NULL,MB_ICONERROR|MB_OK);
return FALSE;
}
UnlockResource(bmpgl);
FreeResource(bmpgl);
return TRUE;
}
biBitCount=24 表示位图最多有2^24种颜色。这种位图没有调色板(bmiColors成员尺寸为0),在位数组中,每3个字节代表一个象素,分别对应于颜色R、G、B。
那么调色板的尺寸该如何计算,在写入文件时的偏移又该如何重新计算?
看了半天bmp文件结构的文章了,还是搞不明白....
本来是想在得到BITMAPINFOHEADER后直接创建DIB,然后用DC画在对话框上但后来发现画的东西在对话框响应WM_PAINT消息时会被擦除掉,所以决定多走一步,先保存成位图,然后再显示.