I. Introduction The JPEG image compression standard is a Lossy image compression standard. However, the image quality after compression is basically unchanged due to the insensitivity of human eyes, soon, it was widely recognized with a high compression rate. Although the GIF format only supports 256 colors, it has a high compression ratio for images with less colors and even exceeds the JPEG standard, which has been widely recognized. However, Microsoft Visual C ++ 6.0, an important development tool for many programmers, only provides good support for BMP bitmap files without any compression, you can read, display, store, or even create a memory bitmap in the memory. Because BMP images are not compressed, both as external files of the program and as internal resources of the program occupy a large amount of space, in particular, the latter will greatly increase the length of the executable file. It can be seen that if you can replace BMP files with JPEG or GIF images that have been compressed and have a good compression ratio in VC, it will undoubtedly be very attractive. Ii. Design Ideas Although there are some Active X controls for operations, processing JPEG, GIF, and other formats of images, it is not very convenient to use in general. I have explored through experiments, A simple method to implement the above functions by using the OLE method of the COM interface is summarized. The following is an introduction to the readers: Now we want to use ipicture's COM interface. It is necessary to understand this image interface: this interface mainly manages image objects and their attributes, image objects provide a language-independent abstraction for bitmaps, icons, and elements. Like Standard font objects, the system also provides standard implementations for image objects. The main interfaces are ipicture and ipicturedisp. The latter is derived from the idispatch interface for automatic access to image attributes. Image objects also support the external interface ipropertypolicysink, so that users can make decisions when the image attributes change. The image object also supports the ipersiststream interface, so it can save and load itself from an instance object of an istream interface, and the istream interface also supports data reading and writing of the stream object. We can use the OleLoadPicture function to load images from a stream containing image data. This function simplifies the process of creating a stream-based image object. You can create a new image object and initialize it with the content in the stream. Its function prototype is: STDAPI OleLoadPicture (IStream * pStream, // pointer to a stream containing image data LONG lSize, // number of bytes read from the stream BOOL fRunmode, // The Initial Value REFIID riid corresponding to the image attribute, // Interface ID involved, which describes the type of the interface pointer to be returned VOID ppvObj // the address of the interface pointer variable used in rrid ); Iii. Implementation Before displaying the image, you must first obtain the storage path of the image file. Here, the standard file opening dialog box is used to select the image file. The file name is stored in the CString variable m_sPath: CFileDialog dlg (TRUE, "jpg", "*. jpg ", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "JPEG file (*. jpg) | *. jpg | GIF file (*. gif) | *. gif |", NULL ); If (dlg. DoModal () = IDOK) { M_sPath = dlg. GetPathName (); Invalidate (); } For the sake of simplicity, the graphic display code is directly written in OnDraw in the video class. First, open the file and determine the file availability, and put the file content in the object pStm of IStream: IStream * pStm; CFileStatus fstatus; CFile file; LONG cb; ...... If (file. Open (m_Path, CFile: modeRead) & file. GetStatus (m_Path, fstatus) & (cb = fstatus. m_size )! =-1 )) { HGLOBAL hGlobal = GlobalAlloc (GMEM_MOVEABLE, cb ); LPVOID pvData = NULL; If (hGlobal! = NULL) { If (pvData = GlobalLock (hGlobal ))! = NULL) { File. ReadHuge (pvData, cb ); GlobalUnlock (hGlobal ); CreateStreamOnHGlobal (hGlobal, TRUE, & pStm ); } } } Then, you can directly call the OleLoadPicture function to load images from the stream: IPicture * pPic; ...... OleLoadPicture (pStm, fstatus. m_size, TRUE, IID_IPicture, (LPVOID *) & pPic )); Because this function sometimes fails, you should use the SUCCEEDED macro to do some appropriate protection work. The following image display work can continue only when the data load is successful: If (SUCCEEDED (OleLoadPicture (pStm, fstatus. m_size, TRUE, IID_IPicture, (LPVOID *) & pPic ))) { OLE_XSIZE_HIMETRIC hmWidth; OLE_YSIZE_HIMETRIC hmHeight; PPic-> get_Width (& hmWidth ); PPic-> get_Height (& hmHeight ); Double fX, fY; ...... FX = (double) pDC-> GetDeviceCaps (HORZRES) * (double) hmWidth/(double) pDC-> GetDeviceCaps (HORZSIZE) * 100.0 ); FY = (double) pDC-> GetDeviceCaps (VERTRES) * (double) hmHeight/(double) pDC-> GetDeviceCaps (VERTSIZE) * 100.0 ); If (FAILED (pPic-> Render (* pDC, 0, 0, (DWORD) fX, (DWORD) fY, 0, hmHeight, hmWidth,-hmHeight, NULL ))) AfxMessageBox ("rendering image failed! "); PPic-> Release (); } Else AfxMessageBox ("failed to load image from stream! "); The display is mainly completed by the Render function of the IPicture interface object, which is used to draw the specified part of the image to the specified location of the specified device environment. The prototype is as follows: HRESULT Render (HDC hdc, // device environment handle for rendering Images 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 metactor ); Conclusion: by now, the above Code has been able to display JPEG, GIF, and other standard images in the customer area of the program, but for images with multiple frames (I .e., animated) currently, only the first frame can be displayed. To completely display the whole process of GIF animation, you also need the support of the external Active X control. |