Common solutions for JPG, dynamic GIF, and BMP

Source: Internet
Author: User
Common method 0: Use the gif89a class:

Advantage: it is easy and convenient to use dynamic GIF statements.

Disadvantage: Sometimes there will be edge effects, and some images will be quite obvious.

Usage:
# Include "gif89a. H"
Cgif89a * GIF;
Add:
GIF = new cgif89a (this-> m_hwnd );
GIF-> load ("test.gif ");
GIF-> setposition (100,100 );
GIF-> play ();

// GIF-> pause (true); // pause (true) is used to pause GIF playback.
// GIF-> pause (false); // pause (false) indicates that GIF playback continues.

Common Method 1: Use ipicture:

Advantage: In the early stages of MS, images can be used to display all static images. To display dynamic GIF images, you still need a GIF Image Storage Structure, gifimage. If you are interested, you can refer to Xue bi'sHow to display dynamic GIF images in VC

Disadvantages: it is not convenient for users to define their own images. Xue bi's article describes how to edit and display dynamic GIF images in RC static mode. He originally planned to rewrite the GIF class and found gif89a, no additional work

Call method:
Cstring Spath;

Spath = _ T ("1.jpg ");
// CDC * PDC = getdlgitem (idc_control)-> getdc (); // obtain the control DC
Crect zcrect;
Getclientrect (& zcrect );
Showjpggif (PDC, Spath, zcrect. Left, zcrect. Top, zcrect. Width (), zcrect. Height ());

Function implementation

Bool showjpggif (CDC * PDC, cstring strpath, int X, int y, int width, int height)

{

Istream * PSTM;

Cfilestatus fstatus;

Cfile file;

Long CB;

// Open the file and check the file Validity

If (file. Open (strpath, cfile: moderead )&&

File. getstatus (strpath, fstatus )&&

(Cb = fstatus. m_size )! =-1 ))

{

Hglobal = globalalloc (gmem_moveable, CB );

Lpvoid pvdata = NULL;

If (hglobal! = NULL)

{

Pvdata = globallock (hglobal );

If (pvdata! = NULL)

{

File. Read (pvdata, CB );

Globalunlock (hglobal );

Createstreamonhglobal (hglobal, true, & PSTM );

}

}

}

Else

{

Return false;

} // The end of opening the file

 

// Display JPEG and GIF images. a GIF can only display one frame, but not an animation,

// Use the active controller to display animated GIF images.

Ipicture * PPIC;

// Load image from file stream

If (succeeded (oleloadpicture (PSTM, fstatus. m_size, true, iid_ipicture, (lpvoid *) & PPIC )))

{

Ole_xsize_himetric hmwidth;

Ole_ysize_himetric hmheight;

PPIC-> get_width (& hmwidth );

PPIC-> get_height (& hmheight );

Double FX, FY;

// Get image height and width

FX = (double) PDC-> getdevicecaps (horzres) * (double) hmwidth/

(Double) PDC-> getdevicecaps (horzsize) * 100.0;

FY = (double) PDC-> getdevicecaps (vertres) * (double) hmheight/

(Double) PDC-> getdevicecaps (vertsize) * 100.0;

// Use render function display image

If (failed (PPIC-> render (* PDC, X, Y, width, height, 0,

Hmheight, hmwidth,-hmheight, null )))
{

PPIC-> release ();

Return false;

}

PPIC-> release ();

}

Else

{

Return false;

}

Return true;

}

Common Method 2: Use MS cimage

Advantage: The latest image class of MS, which is commonly used by MFC/ATL, should be reliable and easy to use and flexible. The purpose should be to replace ipicture class

Disadvantage: the problem of dynamic GIF still cannot be solved perfectly. # include <atlimage. h>

Cimage m_image;

// PS: Insert the following call Function
Cstring strfilter;
Csimplearray <guid> aguidfiletypes;
Hresult;
// Cimage m_image;

// Obtain the filter string of the image files supported by cimage
Hresult = m_image.getexporterfilterstring (strfilter, aguidfiletypes, _ T ("all image files "));
If (failed (hresult ))
{
MessageBox ("getexporterfilter call failed! ");
Return;
}
Cfiledialog DLG (true, null, null, ofn_filemustexist, strfilter );
If (idok! = DLG. domodal ())
Return;

M_image.destroy ();
// Load the external image file to the cimage object
Hresult = m_image.load (DLG. getfilename ());
If (failed (hresult ))
{
MessageBox ("An error occurred while calling the image file! ");
Return;
}

// Set the title bar of the Main Window
Cstring STR;
Str. loadstring (afx_ids_app_title );
Afxgetmainwnd ()-> setwindowtext (STR + "-" + DLG. getfilename ());

Invalidate (); // force call ondraw

// PS: insert the redraw function below

Void ctemp7view: ondraw (CDC * PDC)
{
Ctemp7doc * pdoc = getdocument ();
Assert_valid (pdoc );

// Todo: add the drawing code for the local data here
If (! M_image.isnull ())
{
Crect zcrect;
Getclientrect (& zcrect );
M_image.draw (PDC-> m_hdc, zcrect. Left, zcrect. Top, zcrect. Width (), zcrect. Height ());
}

}
 

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.