Use the cimage class to display PNG, JPG, and other images

Source: Internet
Author: User
Tags transparent color

System Environment: Windows 7

Software environment: Visual Studio 2008 SP1

Objective: To display the image effect in a single VC document or dialog box Program

Cimage
Is VC. is a type of MFC/ATL sharing class defined in. net, which is also a tool class of ATL. It provides enhanced (DDB and DIB) bitmap support, you can mount, display, convert, and save image files in various formats, including BMP, GIF, JPG, PNG, and TIF. Cimage is an independent class with no base class. (The cimage class is based on GDI + and is introduced from VC. net, which is not found in VC 6.0 .)
ATL
(Active Template Library) is a set of template-Based C ++ classes to simplify small and fast com
Object writing.
To use the cimage class in the MFC program, the image header file atlimage. h must be included: (not included in vs08 SP1)

# Include
<Atlimage. h>

This is a powerful image processing class. The following describes the pictures displayed in the document and dialog box.

For single-document programs:


Step 1: add the header file (because I am using vs2008sp1, I did not add the header file above .)
Step 2: declare an object in the doc header file, such as cimage img
;
Step 3: serialize the serialize function in the doc implementation function

Void cimagepngdoc: serialize (carchive & AR)
{
If (AR. isstoring ())
{
// Todo: add storage code here
}
Else
{
// Todo: Add and load code here
If (! IMG. isnull () IMG. Destroy ();
IMG. Load (AR. GetFile ()-> getfilepath ());

}
}

Step 4: In the ondraw () implementation file of the view

Void cimagepngview: ondraw (CDC * PDC
)
{
Cimagepngdoc * pdoc = getdocument ();
Assert_valid (pdoc );
If (! Pdoc)
Return;

// Todo: add the drawing code for the local data here
If (! Pdoc-> IMG. isnull () pdoc-> IMG. Draw (PDC-> m_hdc, 0, 0 );

}

Complete. The above shows most image formats.

For dialog box programs:


Step 1: declare an object in the header file of the dialog box, such as cimage img
;
Step 2: In the implementation function onpaint () of the dialog box

Void ctestdlg: onpaint ()
{
If (isiconic ())
{
Cpaintdc DC (this); // device context used for plotting

Sendmessage (wm_iconerasebkgnd, reinterpret_cast <wparam> (DC. getsafehdc (), 0 );

// Center the icon in the rectangle of the Workspace
Int cxicon = getsystemmetrics (sm_cxicon );
Int cyicon = getsystemmetrics (sm_cyicon );
Crect rect;
Getclientrect (& rect );
Int x = (rect. Width ()-cxicon + 1)/2;
Int y = (rect. Height ()-cyicon + 1)/2;

// Draw the icon
DC. drawicon (X, Y, m_hicon );
}
Else
{
Cpaintdc DC (this );
If (! IMG. isnull () IMG. Destroy ();
IMG. Load (_ T ("image 3.png "));
If (! IMG. isnull () IMG. Draw (DC. m_hdc, 0, 0 );

Cdialog: onpaint ();
}
}

Complete. The preceding Implementation dialog box shows most images.

Note that only the load () method can be used to open other image formats. If it is used to load resources, it cannot be displayed. The following describes how to make PNG transparent:

If (! IMG. isnull () IMG. transparentblt
(Dc. m_hdc, 255,255,255, IMG. getwidth (), IMG. getheight (), RGB ));

Generally, the transparent color is white, that is, you only need to set the key color to white. There are a lot of related information on the Internet. For details, refer:
Display transparent PNG Image http://blog.csdn.net/wormsun/archive/2008/11/13/3293741.aspx with cimage

PNG transparent background display path http://m9551.blog.sohu.com/29092953.html

Cimage http://hi.baidu.com/cauciee/blog/item/3053490994877438e8248822.html

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.