Two Methods for loading JPG/GIF/PNG images under VC

Source: Internet
Author: User

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 an example. Code (Note: for example, the error handling is omitted 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 cximage version 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 ();
}
}
}

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.