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 ();