Use the ipicture interface to display images

Source: Internet
Author: User

Use the ipicture interface to display images
The following code uses the ipicture interface to display images, which can be directly stored in the ondraw function.

The ipictrue interface supports the display of BMP, Dib, EMF, GIF, ICO, JPG, and WMF images. However, only BMP and ICO images can be saved.

// Pdoc is the Document Object Pointer
// The PDC is the pointer to the device description table.

: Coinitialize (null); // COM Initialization
Hresult hr;
Cfile file;

File. Open (pdoc-> getpathname (), cfile: moderead | cfile: sharedenynone); // read the file content
DWORD dwsize = file. getlength ();
Hglobal hmem =: globalalloc (gmem_moveable, dwsize );
Lpvoid lpbuf =: globallock (hmem );
File. readhuge (lpbuf, dwsize );
File. Close ();
: Globalunlock (hmem );

Istream * pstream = NULL;
Ipicture * ppicture = NULL;

// Obtain istream from hglobal. The parameter true indicates that the memory is released while istream is released.
HR =: createstreamonhglobal (hmem, true, & pstream );
Assert (succeeded (HR ));

HR =: oleloadpicture (pstream, dwsize, true, iid_ipicture, (lpvoid *) & ppicture );
Assert (hR = s_ OK );

Long nwidth, nheight; // width and height, mm_himetric mode, in 0.01mm
Ppicture-> get_width (& nwidth); // width
Ppicture-> get_height (& nheight); // high

Crect rect;
Getclientrect (& rect );

Csize SZ (nwidth, nheight );
PDC-> himetrictodp (& sz); // converts the mm_himetric mode to the mm_text pixel unit.
Long X, Y, CX, Cy;

// Original size
/*
Cx = Sz. CX;
Cy = Sz. Cy;
X = rect. Width ()/2-CX/2;
Y = rect. Height ()/2-cy/2;
*/

// Automatically adapt to the window
Double fratepic, fratewnd;
Fratepic = (double) SZ. CX/(double) SZ. Cy;
Fratewnd = (double) rect. Width ()/(double) rect. Height ();
If (fratepic> fratewnd)
{
Cx = rect. Width ();
Cy = (long) (rect. Width ()/fratepic );
}
Else
{
Cx = (long) (rect. Height () * fratepic );
Cy = rect. Height ();
}
If (Cx = rect. Width ())
{
X = 0;
Y = rect. Height ()/2-cy/2;
}
If (Cy = rect. Height ())
{
X = rect. Width ()/2-CX/2;
Y = 0;
}

Ppicture-> render (PDC-> m_hdc, X, Y, CX, Cy,
0, nheight, nwidth,-nheight, null );

If (ppicture) ppicture-> release (); // release ipicture pointer
If (pstream) pstream-> release (); // release the istream pointer and release hmem

: Couninitialize ();
 

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.