方法1:直接通過文件創(chuàng)建 LPCSTR szFileUrl; IPicture *pIPicture; OleLoadPicturePath(CComBSTR(szFileUrl), (LPUNKNOWN)NULL, 0, 0, IID_IPicture, (LPVOID*)&pIPicture)) 方法2:通過IStream來創(chuàng)建 // 加載電子地圖 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);// 開辟大內(nèi)存 if(hGlobal != NULL) { LPVOID pvData = NULL; pvData = GlobalLock(hGlobal); if(pvData != NULL) { file.ReadHuge(pvData, lFileSize); // 將圖片數(shù)據(jù)保存在內(nèi)存中 GlobalUnlock(hGlobal); CreateStreamOnHGlobal(hGlobal, TRUE, &m_pIStream); // 創(chuàng)建流 file.Close(); } else { GlobalFree(hGlobal); return; } } else { file.Close(); return; } } else { //打開圖片失敗 return; } OleLoadPicture(m_pIStream, fstatus.m_size, TRUE, IID_IPicture, (LPVOID*)&m_pIPicture); GlobalFree(hGlobal); 2、通過IPicture來獲取圖片的尺寸
OLE_XSIZE_HIMETRIC hmPicWidth; OLE_YSIZE_HIMETRIC hmPicHeight; pIPicture->get_Width(&hmPicWidth); // 獲取圖像寬度 pIPicture->get_Height(&hmPicHeight); // 獲取圖像高度 // 轉(zhuǎn)化單位為象素 nPicWidth = MulDiv(hmPicWidth, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSX), HIMETRIC_PER_INCH); nPicHeight = MulDiv(hmPicHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), HIMETRIC_PER_INCH); 3、通過IPicture來畫圖
void ShowPicture(CDC *pDC) { OLE_XPOS_HIMETRIC xSrc; // 圖片中當(dāng)前顯示區(qū)域的x OLE_YPOS_HIMETRIC ySrc; // 圖片中當(dāng)前顯示區(qū)域的y OLE_XSIZE_HIMETRIC cxSrc; // 圖片中當(dāng)前顯示區(qū)域的寬度 OLE_YSIZE_HIMETRIC cySrc; // 圖片中當(dāng)前顯示區(qū)域的高度 m_pIPicture->Render(*pDC, 0, 0, m_rcBox.Width(), m_rcBox.Height(), xSrc, ySrc, cxSrc, cySrc, NULL); } 4、將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 Be In a 512 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、將IPicture中圖片按規(guī)定的尺寸保存 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; } 本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/crearo/archive/2006/10/10/1328974.aspx |
|