现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

IPicture总结

2013-03-18 22:36 工业·编程 ⁄ 共 4760字 ⁄ 字号 暂无评论

1、IPicture接口对象的创建
         方法1:直接通过文件创建
LPCSTR szFileUrl;
IPicture *pIPicture;
OleLoadPicturePath(CComBSTR(szFileUrl), 
   (LPUNKNOWN)NULL, 
   0, 
   0, 
   IID_IPicture,
   (LPVOID*)&pIPicture))
        方法2:通过IStream来创建
// 加载电子地图
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);// 开辟大内存
  if(hGlobal != NULL)
  {
   LPVOID pvData = NULL;
   pvData = GlobalLock(hGlobal);
   if(pvData != NULL)
   { 
    file.ReadHuge(pvData, lFileSize);   // 将图片数据保存在内存中
    GlobalUnlock(hGlobal);
    CreateStreamOnHGlobal(hGlobal, TRUE, &m_pIStream);  // 创建流
    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); // 获取图像高度
// 转化单位为象素
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;  // 图片中当前显示区域的x
OLE_YPOS_HIMETRIC ySrc;  // 图片中当前显示区域的y
OLE_XSIZE_HIMETRIC cxSrc; // 图片中当前显示区域的宽度
OLE_YSIZE_HIMETRIC cySrc; // 图片中当前显示区域的高度
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中图片按规定的尺寸保存
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;
}

给我留言

留言无头像?