Home   Index   About
Ultimate Pack


Custom Search
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;


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun