MFC: simple use of CImage in MFC; use of mfccimage

Source: Internet
Author: User

MFC: simple use of CImage in MFC; use of mfccimage

 

First, set # include <atlimage. h> Add. At the beginning, I added stdafx. h, but always prompts windows. h is repeatedly introduced, and then added to other header files .. --!

I. image loading

If you want to display an existing image on the interface, you need to load the image to the CImage object. CImage provides four loading functions:

HRESULT Load (LPCTSTR pszFileName) throw ();

HRESULT Load (IStream * pStream) throw ();

Void LoadFromResource (HINSTANCE hInstance, LPCTSTR pszResourceName) throw ();

Void LoadFromResource (HINSTANCE hInstance, UINT nIDResource) throw ();

If the image to be displayed needs to be changed when the program is running, the first function Load (LPCTSTR pszFileName) is usually used to Load the image. The parameter pszFileName specifies the image file to be loaded; if the image to be displayed is fixed, it is usually loaded using the third LoadFromResource (HINSTANCE hInstance, LPCTSTR pszResourceName) or the fourth function LoadFromResource (HINSTANCE hInstance, UINT nIDResource, these two functions obtain image information from the resource. Each parameter is the module instance handle containing the image to be loaded, and the second parameter is the resource ID or name.

The type of LPCTSTR can be understood as const char *

However, I need to add "XX.jpg"

The following code loads images through Load and LoadFromResource respectively:

CImage m_image1; // declared as a class member in the actual code

CImage m_image2; // declared as a class member in the actual code

M_image1.Load ("G: \ xxx.jpg ");

M_image2.LoadFromResource (AfxGetInstanceHandle (), MAKEINTRESOURCE (IDB_BITMAP1 ));

In practice, the first method may fail to load images. At present, I am not quite clear about the reason.

 

Ii. Image Display

The purpose of loading an image into a CImage object is to display it on the interface. The core function used to display the image is Draw. Draw provides six overload functions, it is easy to understand the meaning of parameters. The prototype is as follows:

 

BOOL Draw (HDC hDestDC, int xDest, int yDest, int nDestWidth, int nDestHeight,

Int xSrc, int ySrc, int nscwidth, int nscheight) const throw ();

BOOL Draw (HDC hDestDC, const RECT & rectDest, const RECT & rectSrc) const throw ();

BOOL Draw (HDC hDestDC, int xDest, int yDest) const throw ();

BOOL Draw (HDC hDestDC, const POINT & pointDest) const throw ();

BOOL Draw (HDC hDestDC, int xDest, int yDest, int nDestWidth, int nDestHeight) const throw ();

BOOL Draw (HDC hDestDC, const RECT & rectDest) const throw ();

 

To display an image in the control size, use the following code:

If (m_image2.IsNull () // determines whether an image exists.
Return;
// Obtain the size of the customer Zone
CRect zcRect;
GetDlgItem (IDC_STATIC_PIC2)-> GetClientRect (& zcRect );
// Display the image on the Interface
M_image2.Draw (GetDlgItem (IDC_STATIC_PIC2)-> GetDC ()-> m_hDC,
ZcRect. left,
ZcRect. top,
ZcRect. Width (),
ZcRect. Height ());

Of course, it can be very simple to use

Image-> Draw (this-> GetDC ()-> m_hDC, 0, 0 );

 

You can use image-> SetPixel (10, 10, RGB (, 0) to change the (10, 10) pixels on the image to red.

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.