Only VC provides corresponding APIs and classes to operate BMP bitmaps, icons, and (enhancement) metafiles, but does not support images in JPG, GIF, and PNG formats, these formats are often used. Here we will introduce two methods to operate images in these formats. 1. Use API oleloadpicture to load images in JPG and GIF formats (Note: The PNG format is not supported. In addition, only the first frame can be loaded in GIF format, but not transparent) The oleloadpicture function actually creates an ipicture-type COM interface object, and then we can use this COM interface to operate the image (in fact, you can also use the API olecreatepictureindirect to load the image, however, the oleloadpicture function simplifies the creation of stream-based ipicture objects. The following is the sample code: (Note: As it is only used for examples, error handling is not saved in the Code) /* * The following code snippet allows you to read and display images from a specified path. */ Void displayimage (HDC, lpctstr szimagepath) { Handle hfile = createfile (szimagepath, generic_read, file_share_read, null, open_existing, file_attribute_normal, null); // read the handle file from the specified path szimagepath DWORD dwfilesize = getfilesize (hfile, null); // obtains the image file size, which is used to allocate global memory. Hglobal himagememory = globalalloc (gmem_moveable, dwfilesize); // allocate global memory to images Void * pimagememory = globallock (himagememory); // lock the memory DWORD dwreadedsize; // Save the size of the file actually read Readfile (hfile, pimagememory, dwfilesize, & dwreadedsize, null); // read the image to the global memory. Globalunlock (himagememory); // unlock memory Closehandle (hfile); // close the file handle Istream * pistream; // create an istream interface pointer to save the image stream. Ipicture * pipicture; // create an ipicture interface pointer to indicate the image object. Createstreamonhglobal (himagememory, false, & pistream) // use the istream interface pointer in the global memory. Oleloadpicture (pistream, 0, false, iid_ipicture, (lpvoid *) & (pipicture); // obtain ipicture interface pointer using oleloadpicture // After obtaining the ipicture COM interface object, you can obtain image information and display images. Ole_xsize_himetric hmwidth; Ole_ysize_himetric hmheight; Pipicture-> get_width (& hmwidth); // obtain the image width and height using the interface Pipicture-> get_height (& hmheight ); Pipicture-> render (HDC, 100,100, 0, hmheight, hmwidth,-hmheight, null); // draw an image on the specified DC Globalfree (himagememory); // release global memory Pistream-> release (); // release pistream Pipicture-> release (); // release pipicture } 2. Operate images using third-party development libraries In this case, I recommend a library named cximage. Cximage contains many classes that can be used to load, save, display, and transform images. It also supports many image formats, including BMP, JPEG, GIF, PNG, Tiff, MNG, ICO, PCX, TGA, WMF, wbmp, jbg, j2k, etc. In addition, cximage supports Alpha channels, animation frames, and many other functions, and it is still open-source and free. The current version of cximage is v6.00. For introduction and download, visit http://www.codeproject.com/kb/graphics/cximage.aspx. The usage of cximage is very simple. The example is as follows (error handling is skipped ): Void displayimage (HDC, cstring filename) { Cstring fileext; // image Extension Int Len = filename. getlength (); For (INT I = len-1; I> = 0; I --) // get the image Extension { If (filename [I] = '.') { Fileext = filename. mid (I + 1 ); Break; } } Fileext. makelower (); // convert the extension to lowercase. If (fileext! = _ T ("")) { // Create a cximage object. The static method cximage: gettypeidfromname is used to obtain the ID representation of the image format based on the extension. Cximage image (filename, cximage: gettypeidfromname (fileext )); If (image. isvalid ()) { Image. Draw (HDC ); Image. Destroy (); } } |