Use cimage to display, copy, draw, and convert images.

Source: Internet
Author: User

Recently, a program compiled using VC needs to display images on the interface. After reading the materials, the program can be implemented using cimage. Sort related technical points as follows for future reference.

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, lpctstr pszresourcename) Throw ();

Void loadfromresource (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, lpctstr pszresourcename) or the fourth function loadfromresource (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 following code loads images through load and loadfromresource respectively:

 

Cimage m_image1; // The class member A is declared in the actual code.

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

M_image1.load ("G :\\ VC display image \ PIC \ aa.jpg ");

M_image2.loadfromresource (AfxGetInstanceHandle (), makeintresource (idb_bitmap1 ));

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:

 

View plaincopy to clipboardprint?
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 ());
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 ());

 

If you want to display the image as the original image size, you can adjust the size of the control by using the setwindowpos function before the image is displayed, so that the image can be displayed as the original size. The following code demonstrates this situation:

 

View plaincopy to clipboardprint?
If (m_image1.isnull ())
Return;

// Adjust the integral control to the same size as the image
Getdlgitem (idc_static_pic)-> setwindowpos (null,
0, 0, m_image1.getwidth (), m_image1.getheight (),
Swp_nomove );

Crect zcrect;
Getdlgitem (idc_static_pic)-> getclientrect (& zcrect );

M_image1.draw (getdlgitem (idc_static_pic)-> getdc ()-> m_hdc,
Zcrect. Left,
Zcrect. Top,
Zcrect. Width (),
Zcrect. Height ());
If (m_image1.isnull ())
Return;

// Adjust the integral control to the same size as the image
Getdlgitem (idc_static_pic)-> setwindowpos (null,
0, 0, m_image1.getwidth (), m_image1.getheight (),
Swp_nomove );

Crect zcrect;
Getdlgitem (idc_static_pic)-> getclientrect (& zcrect );

M_image1.draw (getdlgitem (idc_static_pic)-> getdc ()-> m_hdc,
Zcrect. Left,
Zcrect. Top,
Zcrect. Width (),
Zcrect. Height ());

 

Of course, if you do not care that the widget size must be the same as the image size, you can also choose not to adjust the widget size, but directly set the fourth and fifth parameters when calling the draw method.

 

Note: After the displayed image is covered by another window, or after the current window is minimized, when the program interface is displayed again, the image you just set will have no trace, so you must re-paint them. Therefore, you can put the code for displaying images in a separate function and call the code in onpaint. (I don't know if there is any simpler method. I hope some netizens will tell me ).

 

3. Copying Images

You can also use the bitblt function of cimage to copy an image to another image. The effect is equivalent to placing an image on another image. This feature does not seem to be frequently used. The bitblt function is prototype as follows:

 

Bool bitblt (

HDC hdestdc,

Int xdest,

Int ydest,

DWORD dwrop = srccopy

) Const throw ();

Bool bitblt (

HDC hdestdc,

Const point & pointdest,

DWORD dwrop = srccopy

) Const throw ();

Bool bitblt (

HDC hdestdc,

Int xdest,

Int ydest,

Int ndestwidth,

Int ndestheight,

Int xsrc,

Int ysrc,

DWORD dwrop = srccopy

) Const throw ();

Bool bitblt (

HDC hdestdc,

Const rect & rectdest,

Const point & pointsrc,

DWORD dwrop = srccopy

) Const throw ();

 

For example, to copy the image stored in image2 to image1, you can use the following code:

 

M_image2.bitblt (m_image1.getdc (), 0, 0 );

 

After executing the above Code, you need to re-draw m_image1 on the interface. Otherwise, only the copy effect on the interface can be displayed.

 

4. Drawing in memory

Sometimes you need to dynamically draw on the interface, but if you directly draw on the interface may cause flickering problems, you can first draw in the cimage object, then the cimage object is displayed on the interface to reduce this feeling. Assume that m_image3 is a cimage object, and you need to draw a triangle and display it on the interface. You can use the following code.

 

View plaincopy to clipboardprint?
If (! M_image3.isnull ())
{
M_image3.destroy ();
}

M_image3.create (100,100, 32 );

HDC = m_image3.getdc ();
CDC * PDC = new CDC;
PDC-> attach (HDC );
Cpen penred (ps_solid, 2, RGB (255, 0, 0 ));
Cpen * oldpen = PDC-> SelectObject (& penred );
PDC-> moveTo (50, 10 );
PDC-> lineto (10, 90 );
PDC-> lineto (90, 90 );
PDC-> lineto (50, 10 );
Deleteobject (PDC-> SelectObject (oldpen ));

Releasedc (PDC );

Crect zcrect;
Getdlgitem (idc_static_pic3)-> getclientrect (& zcrect );
M_image3.draw (getdlgitem (idc_static_pic3)-> getdc ()-> m_hdc,
Zcrect. Left,
Zcrect. Top,
Zcrect. Width (),
Zcrect. Height ());
If (! M_image3.isnull ())
{
M_image3.destroy ();
}

M_image3.create (100,100, 32 );

HDC = m_image3.getdc ();
CDC * PDC = new CDC;
PDC-> attach (HDC );
Cpen penred (ps_solid, 2, RGB (255, 0, 0 ));
Cpen * oldpen = PDC-> SelectObject (& penred );
PDC-> moveTo (50, 10 );
PDC-> lineto (10, 90 );
PDC-> lineto (90, 90 );
PDC-> lineto (50, 10 );
Deleteobject (PDC-> SelectObject (oldpen ));

Releasedc (PDC );

Crect zcrect;
Getdlgitem (idc_static_pic3)-> getclientrect (& zcrect );
M_image3.draw (getdlgitem (idc_static_pic3)-> getdc ()-> m_hdc,
Zcrect. Left,
Zcrect. Top,
Zcrect. Width (),
Zcrect. Height ());

 

5. Convert image formats

The load function of cimage can be used to import image files in multiple formats, or we can draw a picture directly in the memory. These image information may eventually need to be converted to the specified image compression format. It is very easy to use the SAVE Function of cimage to complete this conversion. The prototype of the function save is as follows:

 

Hresult save (

Istream * pstream,

Refguid guidfiletype

) Const throw ();

Hresult save (

Lpctstr pszfilename,

Refguid guidfiletype = guid_null

) Const throw ();

 

The guidfiletype parameter specifies the type of the result to be saved. The values and meanings are as follows:

Imageformatbmp uncompressed bitmap

Imageformatpng compression format

Imageformatjpeg compression format

Imageformatgif compression format

 

For the second reload form of the SAVE Function, you can also set guidfiletype to guid_null or not (guidfiletype will be guid_null by default ), in this case, the SAVE Function identifies the format to be saved by the value of the filename suffix specified by the first parameter pszfilename. Animated GIF compression format.

 

M_image1.save ("G: \ aa.bmp ");

M_image1.save ("G: \ aa.gif ");

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ishallwin/archive/2009/11/20/4840180.aspx

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.