The implementation steps are as follows:
1. Click the Browse button to select a jpg image file.
Click the Browse button to open the file dialog box and select the jpg image file to get the path directory of the file.
The key code is as follows:
Char szfilter [] = "JPG files (*. JPG) | *. JPG | JPEG files (*. JPEG) | *. JPEG | GIF files (*. GIF) | *. GIF | all files (*. *) | *. * | ";
Cfiledialog DLG (true, "JPG | JPEG | GIF", null, ofn_hidereadonly | ofn_overwriteprompt, szfilter );
If (DLG. domodal () = idok)
{
Cstring strpathname = DLG. getpathname ();
}
2. Load JPG files to the memory
It is mainly implemented through the com class-ipicture.
The key code is as follows:
Bool cshowjpgdlg: loadjpgfile (const char * ppathname)
{
Cfile file;
If (! File. Open (ppathname, cfile: moderead ))
Return FALSE;
DWORD m_nFileLen;
M_nFileLen = file. GetLength ();
HGLOBAL hMem =: GlobalAlloc (GMEM_MOVEABLE, m_nFileLen );
LPVOID lpBuf =: GlobalLock (hMem );
If (file. ReadHuge (lpBuf, m_nFileLen )! = M_nFileLen)
Return FALSE;
File. Close ();
: GlobalUnlock (hMem );
If (CreateStreamOnHGlobal (hMem, TRUE, & pStream )! = S_ OK)
Return FALSE;
If (oleloadpicture (pstream, m_nfilelen, true, iid_ipicture, (lpvoid *) & ppicture )! = S_ OK)
Return false;
Return true;
}
3. display jpg images on the screen
The render function is used to display jpg images.
Render Function Description
Hresult render (HDC, // device environment handle
Long X, // horizontal coordinates on HDC
Long y, // The vertical coordinates on HDC
Long cx, // Image Width
Long cy, // Image Height
OLE_XPOS_HIMETRIC xSrc, // horizontal offset on the source Image
OLE_YPOS_HIMETRIC ySrc, // vertical offset on the source Image
OLE_XSIZE_HIMETRIC cxSrc, // number of horizontal copies on the source Image
OLE_YSIZE_HIMETRIC cySrc, // number of vertical copies on the source Image
LPCRECT prcWBounds // pointer to the Environment handle of the target metadevice
);
The key code is as follows:
Void CShowJPGDlg: DrawImage (int x, int y, CDC * pDC)
{
If (pPicture! = NULL)
{
Long nWidth, nHeight;
PPicture-> get_Width (& nWidth );
PPicture-> get_Height (& nHeight );
/*
// Original large image display
CSize sz (nWidth, nHeight );
PDC-> HIMETRICtoDP (& sz );
PPicture-> Render (pDC-> m_hDC, 0, 0, sz. cx, sz. cy, 0, nHeight, nWidth,-nHeight, NULL );
*/
// Display by window size
CRect rect;
GetClientRect (& rect );
PPicture-> Render (pDC-> m_hDC, x, y, rect. Width (), rect. Height (), 0, nHeight, nWidth,-nHeight, NULL );
}
}
Note: because it is implemented mainly through the com class-ipicture, do not forget coinitialize (null); and use couninitialize ();.
JPEG, GIF, and jpg are displayed in the same way.
Compared with BMP images, JPG, JPEG, and GIF images have many advantages. However, the processing of BMP images in VC seems to be more of a matter of course. We believe that the processing of such com-based display jpg, JPEG, and GIF images makes us feel refreshed.