|
Opening and Displaying a .BMP File
In the sample application, a user is able to open a .BMP file that contains a
bitmapped image and display that image in the client area of the application's
window. The user selects the file to be opened when the application displays
the Open dialog box. (For more information about the Open dialog box, see Common Dialog Box Library.)
After the user selects a file and closes the dialog box, the file and path
names are stored in members of the OPENFILENAME structure. The application uses this data to open the appropriate file and
retrieve the bitmap header and data. The following code sample can be used to
retrieve this data.
/* Retrieve a handle identifying the file. */
hfbm = CreateFile(ofn.lpstrFile, GENERIC_READ,
FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY,
(HANDLE) NULL);
/* Retrieve the BITMAPFILEHEADER structure. */
ReadFile(hfbm, &bmfh, sizeof(BITMAPFILEHEADER),
&dwRead, (LPOVERLAPPED)NULL);
/* Retrieve the BITMAPFILEHEADER structure. */
ReadFile(hfbm, &bmih, sizeof(BITMAPINFOHEADER),
&dwRead, (LPOVERLAPPED)NULL);
/* Allocate memory for the BITMAPINFO structure. */
hmem1 = GlobalAlloc(GHND,
sizeof(BITMAPINFOHEADER) +
((1<<bmih.biBitCount) * sizeof(RGBQUAD)));
lpbmi = GlobalLock(hmem1);
/*
* Load BITMAPINFOHEADER into the BITMAPINFO
* structure.
*/
lpbmi->bmiHeader.biSize = bmih.biSize;
lpbmi->bmiHeader.biWidth = bmih.biWidth;
lpbmi->bmiHeader.biHeight = bmih.biHeight;
lpbmi->bmiHeader.biPlanes = bmih.biPlanes;
lpbmi->bmiHeader.biBitCount = bmih.biBitCount;
lpbmi->bmiHeader.biCompression = bmih.biCompression;
lpbmi->bmiHeader.biSizeImage = bmih.biSizeImage;
lpbmi->bmiHeader.biXPelsPerMeter = bmih.biXPelsPerMeter;
lpbmi->bmiHeader.biYPelsPerMeter = bmih.biYPelsPerMeter;
lpbmi->bmiHeader.biClrUsed = bmih.biClrUsed;
lpbmi->bmiHeader.biClrImportant = bmih.biClrImportant;
/*
* Retrieve the color table.
* 1 << bmih.biBitCount == 2 ^ bmih.biBitCount
*/
ReadFile(hfbm, lpbmi->bmiColors,
((1<<bmih.biBitCount) * sizeof(RGBQUAD)),
&dwRead, (LPOVERLAPPED) NULL);
/*
* Allocate memory for the required number of
* bytes.
*/
hmem2 = GlobalAlloc(GHND,
(bmfh.bfSize - bmfh.bfOffBits));
lpvBits = GlobalLock(hmem2);
/* Retrieve the bitmap data. */
ReadFile(hfbm, lpvBits,
(bmfh.bfSize - bmfh.bfOffBits),
&dwRead, (LPOVERLAPPED) NULL);
/*
* Create a bitmap from the data stored in the
* .BMP file.
*/
hbm = CreateDIBitmap(hdc, &bmih,
CBM_INIT, lpvBits,
lpbmi, DIB_RGB_COLORS);
/*
* Unlock the global memory objects and
* close the .BMP file.
*/
GlobalUnlock(hmem1);
GlobalUnlock(hmem2);
CloseHandle(hfbm);
/* Set the fDisplayBitmap flag. */
if (hbm)
fDisplayBitmap = TRUE;
else
TextOut(hdc, 100, 100, "LoadBitmap Failed", 17);
/* Paint the window (and draw the bitmap). */
GetClientRect(hwnd, &rect);
InvalidateRect(hwnd, &rect, TRUE);
UpdateWindow(hwnd);
Once the bitmap data is retrieved, the bitmapped image can be drawn in the
application's client area. The following code sample is used to draw the bitmap.
case WM_PAINT:
BeginPaint(hwnd, &ps);
if (fDisplayBitmap) {
hdcMem = CreateCompatibleDC(ps.hdc);
SelectObject(hdcMem, hbm);
GetObject(hbm, sizeof(BITMAP), (LPSTR) &bm);
BitBlt(ps.hdc, 0, 0, bm.bmWidth, bm.bmHeight,
hdcMem, 0, 0, SRCCOPY);
DeleteDC(hdcMem);
}
EndPaint(hwnd, &ps);
break;
Related Links
Software for Delphi and C++ Builder developers
Software for Visual Studio .NET developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET
More Online Helps
Win32 Multimedia Programmer's Reference (mmedia.hlp)
OLE Programmer's Reference (ole.hlp)
Microsoft Windows Pen API Programmer's Reference (penapi.hlp)
Microsoft Windows Sockets 2 Reference (sock2.hlp)
Microsoft Windows Telephony API (TAPI) Programmer's Reference (tapi.hlp)
Unix Manual Pages
|