1. Creation of IPicture interface objects
Method 1: Create directly from the file
LPCSTR Szfileurl;
IPicture *pipicture;
Oleloadpicturepath (CComBSTR (Szfileurl),
(Lpunknown) Null
0,
0,
Iid_ipicture,
(lpvoid*) &pipicture))
Method 2: Create by using IStream
Load e-Map
LPCSTR Szfileurl;
IStream *pstream = NULL;
IPicture *pipicture = NULL;;
CFileStatus Fstatus;
CFile file;
LONG lfilesize;
Hglobal Hglobal;
if (file. Open (Szfileurl, cfile::moderead) && file. GetStatus (Szfileurl, Fstatus)
&& ((lfilesize = fstatus.m_size)! =-1))
{
Hglobal = GlobalAlloc (gmem_moveable, lfilesize);//Open up large memory
if (hglobal! = NULL)
{
LPVOID pvData = NULL;
PvData = GlobalLock (hglobal);
if (pvData! = NULL)
{
File. Readhuge (PvData, lfilesize); Save picture data in memory
GlobalUnlock (HGLOBAL);
CreateStreamOnHGlobal (Hglobal, TRUE, &m_pistream); Create a Stream
File. Close ();
}
Else
{
GlobalFree (HGLOBAL);
Return
}
}
Else
{
File. Close ();
Return
}
}
Else
{
Failed to open picture
Return
}
OleLoadPicture (M_pistream, Fstatus.m_size, TRUE, Iid_ipicture, (lpvoid*) &m_pipicture);
GlobalFree (HGLOBAL);
2, through the ipicture to obtain the size of the picture
Ole_xsize_himetric Hmpicwidth;
Ole_ysize_himetric Hmpicheight;
Pipicture->get_width (&hmpicwidth); Get Image width
Pipicture->get_height (&hmpicheight); Get Image Height
Conversion Unit is pixel
Npicwidth = MulDiv (Hmpicwidth, GetDeviceCaps (GetDC ()->M_HDC, logpixelsx), himetric_per_inch);
Npicheight = MulDiv (Hmpicheight, GetDeviceCaps (GetDC ()->M_HDC, logpixelsy), himetric_per_inch);
3, through the IPicture to draw
void Showpicture (CDC *PDC)
{
Ole_xpos_himetric xsrc; X of the current display area in the picture
Ole_ypos_himetric ysrc; Y of the current display area in the picture
Ole_xsize_himetric cxsrc; The width of the current display area in the picture
Ole_ysize_himetric cysrc; The height of the current display area in the picture
M_pipicture->render (*PDC, 0, 0, m_rcbox.width (), M_rcbox.height (), XSRC, Ysrc, Cxsrc, CYSRC, NULL);
}
4, Save the pictures in IPicture.
BOOL savetofilefromipicture (LPCSTR szfilename, IPicture *ppic)
{
Create ILockBytes Buffer
ILockBytes *plkbyt = NULL;
CreateILockBytesOnHGlobal (NULL, TRUE, &plkbyt);
Create IStorage
IStorage *pstorage = NULL;
HRESULT hr =:: Stgcreatedocfileonilockbytes (Plkbyt,
stgm_share_exclusive | Stgm_create | Stgm_readwrite, 0, &pstorage);
if (FAILED (HR))
{
Plkbyt->release ();
Plkbyt = NULL;
return FALSE;
}
Create IStream
IStream *pstream = NULL;
hr = Pstorage->createstream (L "Picture",
stgm_share_exclusive | Stgm_create | Stgm_readwrite, 0, 0, &pstream);
if (FAILED (HR))
{
Pstorage->release ();
Pstorage = NULL;
Plkbyt->release ();
Plkbyt = NULL;
return FALSE;
}
Copy Data Stream
Long lsize;
hr = Ppic->saveasfile (PStream, TRUE, &lsize);
if (FAILED (HR))
{
Pstream->release ();
PStream = NULL;
Pstorage->release ();
Pstorage = NULL;
Plkbyt->release ();
Plkbyt = NULL;
return FALSE;
}
Get Statistics for Final Size of Byte Array
STATSTG STATSTG;
hr = Plkbyt->stat (&STATSTG, statflag_noname);
if (FAILED (HR))
{
Pstream->release ();
PStream = NULL;
Pstorage->release ();
Pstorage = NULL;
Plkbyt->release ();
Plkbyt = NULL;
return FALSE;
}
Calculate "Pure" picture Data, must is in a Blocks ...
Double dbskipfloat = (double (lsize)/512);
DWORD dwpicdatasize = 0;
if (Dbskipfloat > DWORD (dbskipfloat))
{
Dwpicdatasize = (DWORD) dbskipfloat + 1;
}
Else
{
Dwpicdatasize = (DWORD) dbskipfloat;
}
Dwpicdatasize = dwpicdatasize * 512;
Allocate only the ' Pure ' picture Data
BYTE *ppicdatabuffer = (byte*) malloc (dwpicdatasize);
if (Ppicdatabuffer = = NULL)
{
Pstream->release ();
PStream = NULL;
Plkbyt->release ();
Pstorage->release ();
Pstorage = NULL;
Plkbyt = NULL;
return FALSE;
}
Read "Pure" Picture Data to Buffer
_ularge_integer Uloffset;
Uloffset.lowpart = 0;
Uloffset.highpart = 0;
Uloffset.quadpart = (DWORD) (statstg.cbsize.quadpart-dwpicdatasize);
DWORD dwrealdatasize;
hr = Plkbyt->readat (Uloffset, Ppicdatabuffer, Dwpicdatasize, &dwrealdatasize);
if (FAILED (HR))
{
Free (ppicdatabuffer);
Ppicdatabuffer = NULL;
Pstream->release ();
PStream = NULL;
Pstorage->release ();
Pstorage = NULL;
Plkbyt->release ();
Plkbyt = NULL;
return FALSE;
}
Save "Pure" Picture Data to File
CFile fbmp;
CFileException e;
if (!fbmp.open (szFileName, Cfile::typebinary | Cfile::modecreate | Cfile::modewrite, &e))
{
Free (ppicdatabuffer);
Ppicdatabuffer = NULL;
Pstream->release ();
PStream = NULL;
Pstorage->release ();
Pstorage = NULL;
Plkbyt->release ();
Plkbyt = NULL;
return FALSE;
}
Fbmp.write (Ppicdatabuffer, dwrealdatasize);
Fbmp.close ();
Free (ppicdatabuffer);
Ppicdatabuffer = NULL;
Pstream->release ();
PStream = NULL;
Pstorage->release ();
Pstorage = NULL;
Plkbyt->release ();
Plkbyt = NULL;
return TRUE;
}
5, save the picture in IPicture as the specified size
Bool cipicturedlg::save2file (Lpcstr szfilename, ipicture *ppic, int nwidth, int nheight)
{
// create a new ipicture
ole_handle hpic = null;
if (FAILED (Ppic->get_handle (&hpic)))
{
return false;
}
HBITMAP hBmp = (HBITMAP) copyimage ((HANDLE) hPic,
image_ bitmap,
nwidth,
nwidth,
lr_ CreateDIBSection);
if (hbmp == null)
{
return false;
}
pictdesc picdesc;
picdesc.cbsizeofstruct = sizeof (PICTDESC);
picDesc.picType = PICTYPE_BITMAP;
picdesc.bmp.hbitmap = hbmp;
IPicture *pnewpic = NULL;
if (SUCCEEDED (Olecreatepictureindirect (&picdesc, Iid_ipicture, FALSE, (LPVOID *) &pnewpic)))
{
Save to File
Save2file (szFileName, pnewpic);
Pnewpic->release ();
Pnewpic = NULL;
DeleteObject (hbmp);
hbmp = NULL;
return TRUE;
}
DeleteObject (hbmp);
hbmp = NULL;
return FALSE;
}
Source: http://blog.csdn.net/crearo/article/details/1328974
Favorite: IPicture Summary