已知 BITMAPINFO 如何保存为BMP文件
代码如下:以下代码能正常显示在C语言的控件面板上,我想将其已知的BITMAPINFO 保存为 BMP文件,如何写,最好写出代码。
static voidPaint(HWND h){ int iSel; HDC hDC; PAINTSTRUCT ps; char bmpBuffer[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)] = { 0 }; BITMAPINFO* pBmpInfo; BYTE* pBmpData; ABS_DWORD i; if(pImage == NULL) return; iSel = TabCtrl_GetCurSel(hwndTab); if(iSel != 0) return; /* Create windows bitmap header in memory for our sample image. */ pBmpInfo = (BITMAPINFO*) bmpBuffer; pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pBmpInfo->bmiHeader.biWidth = pImage->Width; /* The following minus sign tells GDI to draw it in the right vertical * direction: */ pBmpInfo->bmiHeader.biHeight = -((LONG)pImage->Height); pBmpInfo->bmiHeader.biPlanes = 1; pBmpInfo->bmiHeader.biBitCount = 8; pBmpInfo->bmiHeader.biCompression = BI_RGB; /* The colormap of the bitmap has to contain grayscale colors, * spread lineary from black (0) to white (pImage->ColorCount - 1) */ for(i = 0; i < pImage->ColorCount; i++) { BYTE byGray = (BYTE) ((255 * i) / (pImage->ColorCount - 1)); pBmpInfo->bmiColors[i].rgbRed = byGray; pBmpInfo->bmiColors[i].rgbGreen = byGray; pBmpInfo->bmiColors[i].rgbBlue = byGray; } /* Each scan line of the image must end on 4-byte boundary, so if * width of the image is not divisible by 4, we have to rearrange * the bytes in a temporary buffer. */ if(pImage->Width % 4 == 0) { pBmpData = NULL; } else { ABS_DWORD scanLineLen; ABS_DWORD scanLineNo; scanLineLen = pImage->Width + (4 - pImage->Width % 4); pBmpData = (BYTE*) malloc(scanLineLen * pImage->Height); for(scanLineNo = 0; scanLineNo < pImage->Height; scanLineNo++) { memcpy( pBmpData + scanLineLen * scanLineNo, pImage->ImageData + pImage->Width * scanLineNo, pImage->Width ); } } /* Draw the bitmap to the window */ hDC = BeginPaint(h, &ps); StretchDIBits(hDC, 175, 10, pImage->Width, pImage->Height, 0, 0, pImage->Width, pImage->Height, (void*) (pBmpData != NULL ? pBmpData : pImage->ImageData), pBmpInfo, DIB_RGB_COLORS, SRCCOPY ); EndPaint(h, &ps); /* If we used the temporary buffer for image data, we have to release it */ if(pBmpData != NULL) free(pBmpData);}