H
HGallon
I am merging several projects. One of them requires loading bitmaps from
files (OK, I know I can use LoadBitmap () which is deprecated and
LoadImage () which isn't, but I am reading the information in the
BITMAPINFOHEADER to get the image height and width.)
This works:
HANDLE hFile = CreateFile (szFileName, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
.
.
.
// pBitmapInfo is the BITMAPINFOHEADER structure, already read
HBITMAP hBitmap = CreateDIBSection (hDC, pBitmapInfo,
DIB_RGB_COLORS, (PVOID*) &pBitmapBits, NULL, 0);
// bmfHeader is the BITMAPFILEHEADER structure, already read
PBYTE pBitmapBits;
SetFilePointer (hFile, bmfHeader.bfOffBits, NULL, FILE_BEGIN);
DWORD dwBytesRead;
ReadFile (hFile, pBitmapBits, pBitmapInfo->bmiHeader.biSizeImage,
&dwBytesRead, NULL);
This doesn't:
ifstream iFile;
iFile.open (szFileName);
DWORD dwBytesRead;
.
.
.
// pBitmapInfo is the BITMAPINFOHEADER structure, already read
PBYTE pBitmapBits;
HBITMAP hBitmap = CreateDIBSection (hDC, pBitmapInfo,
DIB_RGB_COLORS, (PVOID*) &pBitmapBits, NULL, 0);
// bmfHeader is the BITMAPFILEHEADER structure, already read
// dwBytesRead is count of bytes imported from file stream
iFile.ignore (bmfHeader.bfOffBits - dwBytesRead);
iFile.read ((UCHAR*) pBitmapBits,
pBitmapInfoHeader->bmiHeader.biSizeImage);
The bitmap resulting from the second code fragment is black halfway from
the top down, before the coloured image appears. Evidently, the system
is assuming that 256 RGBQUAD structures initialized to zero (i.e. black)
are pixels rather than palette information.
Has anyone an inkling why the use of input file streams rather than
ReadFile () calls are failing in this respect ?
files (OK, I know I can use LoadBitmap () which is deprecated and
LoadImage () which isn't, but I am reading the information in the
BITMAPINFOHEADER to get the image height and width.)
This works:
HANDLE hFile = CreateFile (szFileName, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
.
.
.
// pBitmapInfo is the BITMAPINFOHEADER structure, already read
HBITMAP hBitmap = CreateDIBSection (hDC, pBitmapInfo,
DIB_RGB_COLORS, (PVOID*) &pBitmapBits, NULL, 0);
// bmfHeader is the BITMAPFILEHEADER structure, already read
PBYTE pBitmapBits;
SetFilePointer (hFile, bmfHeader.bfOffBits, NULL, FILE_BEGIN);
DWORD dwBytesRead;
ReadFile (hFile, pBitmapBits, pBitmapInfo->bmiHeader.biSizeImage,
&dwBytesRead, NULL);
This doesn't:
ifstream iFile;
iFile.open (szFileName);
DWORD dwBytesRead;
.
.
.
// pBitmapInfo is the BITMAPINFOHEADER structure, already read
PBYTE pBitmapBits;
HBITMAP hBitmap = CreateDIBSection (hDC, pBitmapInfo,
DIB_RGB_COLORS, (PVOID*) &pBitmapBits, NULL, 0);
// bmfHeader is the BITMAPFILEHEADER structure, already read
// dwBytesRead is count of bytes imported from file stream
iFile.ignore (bmfHeader.bfOffBits - dwBytesRead);
iFile.read ((UCHAR*) pBitmapBits,
pBitmapInfoHeader->bmiHeader.biSizeImage);
The bitmap resulting from the second code fragment is black halfway from
the top down, before the coloured image appears. Evidently, the system
is assuming that 256 RGBQUAD structures initialized to zero (i.e. black)
are pixels rather than palette information.
Has anyone an inkling why the use of input file streams rather than
ReadFile () calls are failing in this respect ?