With VB's Picture Controls, BMP, JPG, GIF is very convenient, but in VC, MFC can't display directly, so I refer to the online example written a picture display function, I hope that everyone can use. Windows provides a standard way to display BMP, JPG, GIF, you only need to instantize iPicture and its calls, you can read images from the stream via the OLELOADPICTURE function function and create the iPicture object: OLELOADPICTURE (PSTM, DWFileSize , TRUE, IID_IPicture, (LPVOID *) & pPic); it is defined in MSDN in the following: STDAPI OleLoadPicture (IStream * pStream, // Pointer to the stream that contains picture's data LONG lSize, // Number of bytes read from the stream BOOL fRunmode, // The opposite of the initial value of the picture's // property REFIID riid, // Reference to the identifier of the interface // describing the type of interface pointer to return VOID ppvObj // Address of output variable that receives interface / / POINTER Requested In Riid;
The following is an image display function displayPIC in the dialog box CNEWFORMDLG, use it to display a picture on one control, which can display BMP, JPG and GIF files, parameters as follows: char * lpimagefile Image file name HWND HWnd Show Object Handle INT NSCRWIDTH Display Width INT NSCRHEIGHT Display Height
HRESULT CNEWFORMDLG :: DisplayPic (Char * LPIMAGEFILE, HWND HWND, INT NSCRWIDTH, INT NSCRHEIGHT) {HDC HDC_TEMP = :: getdc (hwnd);
IPicture * PPIC; istream * PSTM;
Bool Bresult;
Handle Hfile = NULL; DWORD DWFILESIZE, DWBYTEREAD;
// Open the graphics file in the hard disk hfile = createfile (lpstrfile, generic_read, file_share_read, null, open_existing, file_attribute_normal, null);
IF (hfile! = invalid_handle_value) {dwfilesize = getFileSize (hfile, null); // Get file bytes
IF (dwfilesize == 0xffffff) Return E_FAIL;} else {return e_fail;}
// Allocate allocated storage HGLobal Hglobal = GlobalAlloc (GMEM_MOVEABLE, DWFILESIZE); lpvoid pvdata = null;
IF (hglobal == null) {AFXMessageBox ("Graphic file allocation error."); return e_fail;} = = (pvdata = GlobalLock (hglobal)) == null) // Lock Allocation memory block {AFXMESSAGEBOX ("memory block lock Problem "); Return E_FAIL;
Readfile (HFile, Pvdata, DwFileSize, & dwbyteread, null; // Read files into memory buffers
GlobalUnlock (Hglobal);
IF (CreateStreamonhglobal (Hglobal, True, & PSTM)! = S_OK) {AFXMessageBox ("Flow initialization failed"; Return E_FAIL;}
// Load graphic file BRESULT = OLELOADPICTURE (PSTM, DWFILESIZE, TRUE, IID_IPICTURE, (LPVOID *) & PPIC);
IF (FAILED) {AFXMessageBox ("Graphics File Loading error."); Return E_FAIL;}
OLE_XSIZE_HIMETRIC HMWIDTH; / / The real width OLE_YSIZE_HIMETRIC hmheight; // Picture of the real height PPIC-> get_width (& HMWidth); PPIC-> Get_Height (& Hmheight);
// Output graphic to the screen BRESULT = PPIC-> Render (HDC_TEMP, 0, 0, NSCRWIDTH, NSCRHEIGHT, 0, HMHEIGHT, HMWIDTH, --HMHEIGHT, NULL);
CloseHandle (HFILE); // Close the open file PPIC-> release (); // free memory. Globalfree (hglobal);
if (SUCCEEDED (bResult)) {return S_OK;} else {AfxMessageBox ( "graphics file loading error."); return E_FAIL;}} call as follows: void CNewFormDlg :: OnButton2 () {HWND m_hWnd; GetDlgItem (IDC_STATIC_VIEW, & m_hWnd) DisplayPic ("f: // picture // crystal //crystal521.jpg", m_hwnd, 500, 500);