图片操作

来源:互联网 发布:宁波seo博客 编辑:程序博客网 时间:2024/04/28 20:24

 CStatic m_bmp; 
 CString m_strSourceFile;
 CRect rect1;

IStream *pStm;
    CFileStatus fstatus;
    CFile file;
    LONG cb;
 
 CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  "Jpeg Files (*.jpg;*.jpeg)|*.jpg; *.jpeg|", NULL);
 if (dlg.DoModal() == IDOK)
 {
  m_strSourceFile = dlg.GetPathName();
  if (file.Open(m_strSourceFile,CFile::modeRead)&&file.GetStatus(m_strSourceFile,fstatus)&& ((cb = fstatus.m_size) != -1))
  {   
   HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);   
   LPVOID pvData = NULL;
   if (hGlobal != NULL)
   {   
    pvData = GlobalLock(hGlobal);
    if (pvData != NULL)
    {
     file.ReadHuge(pvData, cb);
     GlobalUnlock(hGlobal);
     CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
    }
   }
  }
  else
  {
   return;
  }
  //??ê?í???
  IPicture *pPic;
  CoInitialize(NULL);
  CDC* pDC = m_bmp.GetDC();    //??μ???ê????tμ?DC
  if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))    
  {
   OLE_XSIZE_HIMETRIC hmWidth;
   OLE_YSIZE_HIMETRIC hmHeight;   
   pPic->get_Width(&hmWidth);
   pPic->get_Height(&hmHeight);     
   double fX,fY;   
   //get image height and width  
   fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);   
   fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);
   //get image position
   m_bmp.GetWindowRect(&rect1);
   //use render function display image  
   if(FAILED(pPic->Render(*pDC,0,0,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))
   {   
    pPic->Release();   
    return;   
   }  
   pPic->Release();   
  }  
  else   
  {  
   return;
  }
  CoUninitialize();
  return; 
 }

原创粉丝点击