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

首頁 > 學院 > 開發設計 > 正文

MFC顯示網絡圖片_IPicture

2019-11-14 11:48:38
字體:
來源:轉載
供稿:網友

轉自:https://www.douban.com/note/181738144/

3. ipicture

IPicture的縮放效果好一點,有兩種方法: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;        //讀取網頁上圖片文件,實際是個CHttpFile指針      CInternetsession session(L"HttpClient");      CFile* httpFile = (CFile*)session.OpenURL(strImgUrl);      if (httpFile!=INVALID_HANDLE_VALUE)      {          dwFileSize= httpFile->GetLength();//獲取文件字節數          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)//鎖定分配內存塊          return E_FAIL;        //把文件讀入內存緩沖區      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; //圖片的真實寬度, 單位為英寸      OLE_YSIZE_HIMETRIC hmHeight; //圖片的真實高度, 單位為英寸      pPic->get_Width(&hmWidth);      pPic->get_Height(&hmHeight);                  //轉換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;                //將圖形輸出到屏幕上(有點像BitBlt)      bResult=pPic->Render(hDC_Temp,rect.left,rect.top,rect.Width(),rect.Height(),          0,hmHeight,hmWidth,-hmHeight,NULL);        pPic->Release();      httpFile->Close();//關閉打開的文件        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; //圖片的真實寬度, 單位為英寸      OLE_YSIZE_HIMETRIC hmHeight; //圖片的真實高度, 單位為英寸      pPic->get_Width(&hmWidth);      pPic->get_Height(&hmHeight);        //轉換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;      //將圖形輸出到屏幕上(有點像BitBlt)      bResult=pPic->Render(hDC_Temp,showRect.left,showRect.top,showRect.Width(),showRect.Height(),          0,hmHeight,hmWidth,-hmHeight,NULL);      pPic->Release();        file.Close();//關閉打開的文件      ar.Close();      arcstream.Release();      if (SUCCEEDED(bResult))      {          return S_OK;      }      else      {          return E_FAIL;      }    }  把網絡圖片下載到本地還有一個小插曲,開始我使用File Open的方式讀取網絡圖片,對于有些圖片是管用的。但是GetLength得到的值有時會很小,直接導致圖片下載不全,OleLoadPicture的時候程序直接卡死了。[cpp] view plain copy 在CODE上查看代碼片CString CListListBox::SavePic(CString strImgUrl)  {      //本地緩存路徑      if(path.IsEmpty())          return L"";      DWORD dwFileSize,dwByteRead;        //讀取網頁上圖片文件,實際是個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();//獲取文件字節數          }          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;  }  后來在網上搜了牛人寫的程序直接拿來用,居然很好用,速度也挺快[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;  }  大功告成,由于開發時間匆忙,很多細節沒有追究,等以后有時間了再補上,這里做個記錄吧。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阜康市| 黄梅县| 中超| 富阳市| 灵山县| 侯马市| 鄯善县| 杭锦旗| 右玉县| 沾化县| 扎兰屯市| 武义县| 祁门县| 汕头市| 东光县| 揭阳市| 瑞丽市| 天镇县| 德钦县| 丽水市| 辽源市| 于田县| 宣化县| 湘潭市| 偏关县| 南平市| 蓝田县| 诏安县| 安阳市| 阿坝县| 青川县| 介休市| 德惠市| 德兴市| 台前县| 普兰店市| 锦屏县| 诸暨市| 孟连| 宜阳县| 安阳县|