접기
//------------------------------------------------------------------------------
// Name: void SaveImage(BITMAPINFOHEADER& bmiHeader, BYTE* pDIB)
// Desc: This method save color 24bit image to bmp file.
//------------------------------------------------------------------------------
// File filter for Save File dialog
#define FILE_FILTER_TEXT_1 \
TEXT("BMP files (*.bmp)\0*.bmp;\0")\
TEXT("All Files (*.*)\0*.*;\0\0")
// Begin default media search at root directory
#define DEFAULT_MEDIA_PATH TEXT("\\\0")
// DIB header
#define DIB_HEADER_MARKER ((WORD)('M'<<8) | 'B')
void SaveImage(BITMAPINFOHEADER& bmiHeader, BYTE* pDIB)
{
char pszFileName[MAX_PATH] = {0,};
// Save File Dialog ...
static OPENFILENAME ofn={0} ;
static BOOL bSetInitialDir = FALSE ;
ofn.lStructSize = sizeof(OPENFILENAME) ;
ofn.hwndOwner = NULL ;
ofn.lpstrFilter = FILE_FILTER_TEXT_1 ;
ofn.lpstrCustomFilter = NULL ;
ofn.nFilterIndex = 1 ;
ofn.lpstrFile = pszFileName ;
ofn.nMaxFile = MAX_PATH ;
ofn.lpstrTitle = TEXT("Save Bitmap File...\0") ;
ofn.lpstrFileTitle = NULL ;
ofn.lpstrDefExt = TEXT("bmp\0") ;
ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
// Remember the path of the first selected file
if (bSetInitialDir == FALSE)
{
ofn.lpstrInitialDir = DEFAULT_MEDIA_PATH ;
bSetInitialDir = TRUE ;
}
else
ofn.lpstrInitialDir = NULL ;
// Retrieve the name of the selected file
if (!GetSaveFileName((LPOPENFILENAME)&ofn))
return;
BITMAPFILEHEADER bmfHeader;
// only 24bit color bitmap
bmfHeader.bfType = DIB_HEADER_MARKER;
bmfHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = sizeof(bmfHeader) + sizeof(BITMAPINFOHEADER) + bmiHeader.biSizeImage;
bmfHeader.bfReserved1 = 0;
bmfHeader.bfReserved2 = 0;
HANDLE fd = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwLength;
if (!WriteFile(fd, (LPSTR)&bmfHeader, sizeof(bmfHeader), &dwLength, NULL))
return;
if (!WriteFile(fd, (LPSTR)&bmiHeader, sizeof(BITMAPINFOHEADER), &dwLength, NULL))
return;
if (!WriteFile(fd, pDIB, bmiHeader.biSizeImage, &dwLength, NULL))
return;
CloseHandle(fd);
}
#undef DIB_HEADER_MARKER
#undef DEFAULT_MEDIA_PATH
#undef FILE_FILTER_TEXT_1
접기