Load image Summary

Source: Internet
Author: User

Friends who use MFC for GDI development must be familiar with the cbitmap class. This class encapsulates hbitmap objects and simplifies API operations on hbitmap. For example, the loadbitmap method can directly load images with specified IDs in resources, however, in many cases, we need to load images from files. The cbitmap class does not provide such a method.

1. Use the API function LoadImage to specify the lr_loadfromfile flag. For example:
HBITMAP hBitmap = (HBITMAP) ::LoadImage(NULL, strPath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);

2. Using COM, I sorted out the Code as follows:
Hbitmap loadimagefromfile (pctstr lpstrfile) {ipicture * PPIC; istream * PSTM; bool bresult; handle hfile = NULL; DWORD dwfilesize, dwbyteread; // open the image file hfile = createfile (lpstrfile, generic_read, file_share_read, null, open_existing, file_attribute_normal, null) in the hard disk; If (hfile! = Invalid_handle_value) {dwfilesize = getfilesize (hfile, null); // get the number of file bytes if (dwfilesize = 0 xffffffff) return NULL;} else {return NULL ;} // allocate a global bucket hglobal = globalalloc (gmem_moveable, dwfilesize); lpvoid pvdata = NULL; If (hglobal = NULL) return NULL; // lock the allocated memory block if (pvdata = globallock (hglobal) = NULL) return NULL; readfile (hfile, pvdata, dwfilesize, & dwbyteread, null ); // read the file into the memory buffer closehandle (hfile); globalunlock (hglobal); createstreamonhglobal (hglobal, true, & PSTM); // load the image file bresult = oleloadpicture (PSTM, dwfilesize, true, iid_ipicture, (lpvoid *) & PPIC); If (failed (bresult) return NULL; hbitmap = NULL; PPIC-> get_handle (unsigned int *) & hbitmap); hbitmap hbitmapret = (hbitmap) copyimage (hbitmap, image_bitmap, 0, 0, lr_copyreturnorg); PPIC-> release (); Return hbitmapret ;}

The experiment shows that this code can load JPG, GIF, and BMP files, but it cannot load PNG files. If you cannot use the framework, you can try this Code. However, because you use Com, remember to initialize com before using it.
Note: This method can be extended to directly read data from the hard disk to load images, that is, to load images with data streams.
3. The third method is to use the cimage sharing class of ATL and MFC to load images.

CImage img;img.Load( strPath );if( !img.IsNull() ){HBITMAP hBitmap = img.Detach();}

This class is very powerful, as can be seen from its thousands of lines of source code. However, the weak water is three thousand, and only one Shard is used. It is used to load images from files, just for a test.

All images I have tested can be loaded quickly. If you use other frameworks, you can study the cimage source code and encapsulate the image loading function, which is more practical and convenient than the second method of using COM to load images.

Others: experts who understand the hbitmap structure can generally directly read image files and analyze data streams. You can extend this method based on the second method. If you are interested, try it.

If you are interested in displaying pictures, refer to my other article: displaying picture summary.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.