国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

MFC顯示網(wǎng)絡(luò)圖片_IPicture

2019-11-14 11:53:09
字體:
供稿:網(wǎng)友

轉(zhuǎn)自:https://www.douban.com/note/181738144/

3. ipicture

IPicture的縮放效果好一點(diǎn),有兩種方法:1)一種是直接顯示不下載圖片到本地,[cpp] view plain copy 在CODE上查看代碼片HRESULT CListListBox::ShowPic(CDC* pDC,CString strImgUrl,CRect rect)  {      HDC hDC_Temp = pDC->GetSafeHdc();      IPicture *pPic;      IStream *pStm;      HRESULT bResult;// = FALSE;      DWord dwFileSize,dwByteRead;        //讀取網(wǎng)頁上圖片文件,實(shí)際是個CHttpFile指針      CInternetsession session(L"HttpClient");      CFile* httpFile = (CFile*)session.OpenURL(strImgUrl);      if (httpFile!=INVALID_HANDLE_VALUE)      {          dwFileSize= httpFile->GetLength();//獲取文件字節(jié)數(shù)          if (dwFileSize==0xFFFFFFFF)              return E_FAIL;      }      else      {          return E_FAIL;      }        //分配全局存儲空間      HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);      LPVOID pvData = NULL;      if (hGlobal == NULL)          return E_FAIL;      if ((pvData = GlobalLock(hGlobal)) == NULL)//鎖定分配內(nèi)存塊          return E_FAIL;        //把文件讀入內(nèi)存緩沖區(qū)      dwByteRead = httpFile->Read(pvData,dwFileSize);      GlobalUnlock(hGlobal);      CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);                  //裝入圖形文件      bResult=OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID*)&pPic);      if(FAILED(bResult))          return E_FAIL;        OLE_XSIZE_HIMETRIC hmWidth; //圖片的真實(shí)寬度, 單位為英寸      OLE_YSIZE_HIMETRIC hmHeight; //圖片的真實(shí)高度, 單位為英寸      pPic->get_Width(&hmWidth);      pPic->get_Height(&hmHeight);                  //轉(zhuǎn)換hmWidth和hmHeight為pixels距離,1英寸=25.4毫米      int nWidth = MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX),2540);      int nHeight = MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY),2540);                //縮放圖片      int nWZoom = nWidth / rect.Width();      int nHZoom = nHeight / rect.Height();      int nZoom = 1;      nZoom = ( nWZoom > nHZoom ) ? nWZoom : nHZoom;      nWidth /= nZoom;      nHeight /= nZoom;      int midW = (rect.left + rect.right) / 2;      int midH = (rect.top + rect.bottom) / 2;      rect.left = midW - nWidth / 2;      rect.right = midW + nWidth / 2;      rect.top = midH - nHeight / 2;      rect.bottom = midH + nHeight / 2;                //將圖形輸出到屏幕上(有點(diǎn)像BitBlt)      bResult=pPic->Render(hDC_Temp,rect.left,rect.top,rect.Width(),rect.Height(),          0,hmHeight,hmWidth,-hmHeight,NULL);        pPic->Release();      httpFile->Close();//關(guān)閉打開的文件        if (SUCCEEDED(bResult))      {          return S_OK;      }      else      {          return E_FAIL;      }    }  2)%20一種是把圖片下載后保存本地再打開。[cpp] view%20plain copy HRESULT CListListBox::ShowPic(CDC* pDC,CString strImgUrl,CRect rect)  {      HDC hDC_Temp = pDC->GetSafeHdc();      HRESULT bResult;// = FALSE;      DWORD dwFileSize = 0,dwByteRead;      CString url = strImgUrl;      CString path = GetPic(strImgUrl);      //if(url.Find(L".jpeg") != -1) return E_FAIL;      if(path.IsEmpty())      {          url = SavePic(url);                  //return FALSE;      }      else      {          url = path;      }      CFile file;      if (!file.Open(url, CFile::modeRead|CFile::shareDenyWrite))      {          return FALSE;      }        CArchive ar(&file, CArchive::load | CArchive::bNoFlushOnDelete);      CArchiveStream arcstream(&ar);      IPicture *pPic;      IStream *pStm = (IStream*)&arcstream ;            //HRESULT hr = OleLoadPicture(pStm, 0, FALSE,IID_IPicture, (LPVOID*)&pPic);          //ASSERT(SUCCEEDED(hr) && m_spIPicture);        //bResult=OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID*)&pPic);      bResult=OleLoadPicture(pStm,0,FALSE,IID_IPicture,(LPVOID*)&pPic);      if(FAILED(bResult))          return E_FAIL;      OLE_XSIZE_HIMETRIC hmWidth; //圖片的真實(shí)寬度, 單位為英寸      OLE_YSIZE_HIMETRIC hmHeight; //圖片的真實(shí)高度, 單位為英寸      pPic->get_Width(&hmWidth);      pPic->get_Height(&hmHeight);        //轉(zhuǎn)換hmWidth和hmHeight為pixels距離,1英寸=25.4毫米      int nWidth = MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX),2540);      int nHeight = MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY),2540);        //縮放圖片      double nWZoom = nWidth / rect.Width();      double nHZoom = nHeight / rect.Height();      double nZoom = 1;      nZoom = ( nWZoom > nHZoom ) ? nWZoom : nHZoom;      nWidth /= nZoom;      nHeight /= nZoom;      int midW = (rect.left + rect.right) / 2;      int midH = (rect.top + rect.bottom) / 2;      CRect showRect(rect);      showRect.left = ((midW - nWidth / 2) < rect.left) ? rect.left : (midW - nWidth / 2);      showRect.right = ((midW + nWidth / 2) > rect.right) ? rect.right : (midW + nWidth / 2);      showRect.top = ((midH - nHeight / 2) < rect.top) ? rect.top : (midH - nHeight / 2);      showRect.bottom = ((midH + nHeight / 2) > rect.bottom) ? rect.bottom : (midH + nHeight / 2);      if(showRect.left < 0 || showRect.right < 0 || showRect.top < 0 || showRect.bottom < 0) return E_FAIL;      //將圖形輸出到屏幕上(有點(diǎn)像BitBlt)      bResult=pPic->Render(hDC_Temp,showRect.left,showRect.top,showRect.Width(),showRect.Height(),          0,hmHeight,hmWidth,-hmHeight,NULL);      pPic->Release();        file.Close();//關(guān)閉打開的文件      ar.Close();      arcstream.Release();      if (SUCCEEDED(bResult))      {          return S_OK;      }      else      {          return E_FAIL;      }    }  把網(wǎng)絡(luò)圖片下載到本地還有一個小插曲,開始我使用File Open的方式讀取網(wǎng)絡(luò)圖片,對于有些圖片是管用的。但是GetLength得到的值有時會很小,直接導(dǎo)致圖片下載不全,OleLoadPicture的時候程序直接卡死了。[cpp] view plain copy 在CODE上查看代碼片CString CListListBox::SavePic(CString strImgUrl)  {      //本地緩存路徑      if(path.IsEmpty())          return L"";      DWORD dwFileSize,dwByteRead;        //讀取網(wǎng)頁上圖片文件,實(shí)際是個CHttpFile指針      CInternetSession session(L"HttpClient");      CStdioFile* httpFile = session.OpenURL(strImgUrl);      if (httpFile!=INVALID_HANDLE_VALUE)      {          for(int i = 0; i < 100; i++)          {              dwFileSize = httpFile->GetLength();//獲取文件字節(jié)數(shù)          }          if (dwFileSize==0xFFFFFFFF)          {              DWORD err = GetLastError();              return L"";          }      }      else      {          return L"";      }        CFile file;      if(file.Open(path,CFile::modeCreate|CFile::modeWrite))      {          while(dwFileSize > 0)          {              BYTE* pvData = new BYTE[dwFileSize];//[100*1024*1024];              memset(pvData, 0, dwFileSize);              dwByteRead = httpFile->Read(pvData,dwFileSize);              file.Write(pvData,dwByteRead);              delete pvData;              dwFileSize -= dwByteRead;          }      }      file.Close();      httpFile->Close();      session.Close();      return path;  }  后來在網(wǎng)上搜了牛人寫的程序直接拿來用,居然很好用,速度也挺快[cpp] view%20plain copy 派生到我的代碼片CString CListListBox::SavePic(CString strImgUrl)  {      DWORD length=0;      BYTE buffer[1024];      memset(buffer,0,1024);      HINTERNET hInternet;        hInternet=InternetOpen(_T("Testing"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);      if (hInternet==NULL)      {          //cout<<_T("Internet open failed!")<<endl;          return L"";      }        HINTERNET hUrl;      hUrl=InternetOpenUrl(hInternet,strImgUrl,NULL,0,INTERNET_FLAG_RELOAD,0);      if (hUrl==NULL)      {          // cout<<_T("Internet open url failed!")<<endl;          InternetCloseHandle(hInternet);          return L"";      }        BOOL hwrite;      DWORD written;      HANDLE hFile;          CString path = CreateLocalPath(strImgUrl);          hFile=CreateFile(path,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);      if (hFile==INVALID_HANDLE_VALUE)      {          //cout<<_T("Create File failed!")<<endl;          InternetCloseHandle(hUrl);          InternetCloseHandle(hInternet);          return L"";      }        BOOL read;      while(1)      {          read=InternetReadFile(hUrl,buffer,sizeof(buffer),&length);          if(length==0)              break;          hwrite=WriteFile(hFile,buffer,sizeof(buffer),&written,NULL);          if (hwrite==0)          {              //cout<<_T("Write to file failed!")<<endl;              CloseHandle(hFile);              InternetCloseHandle(hUrl);              InternetCloseHandle(hInternet);              return L"";          }      }      CloseHandle(hFile);      InternetCloseHandle(hUrl);      InternetCloseHandle(hInternet);      return path;  }  大功告成,由于開發(fā)時間匆忙,很多細(xì)節(jié)沒有追究,等以后有時間了再補(bǔ)上,這里做個記錄吧。
上一篇:cmsg(3) - Linux man page

下一篇:暑假不AC

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平泉县| 鄄城县| 沙坪坝区| 衡阳市| 吕梁市| 广昌县| 营山县| 临泉县| 新民市| 汝南县| 阜新| 手机| 山西省| 大城县| 永年县| 页游| 徐闻县| 上饶县| 石泉县| 澄迈县| 扎鲁特旗| 赤城县| 民县| 读书| 巴青县| 锡林郭勒盟| 名山县| 合肥市| 汶川县| 和平县| 交城县| 抚宁县| 西贡区| 额尔古纳市| 寿宁县| 顺义区| 漳平市| 镶黄旗| 东城区| 邛崃市| 阜新市|