ShowPicture

 

#include <OCIDL.h>
#include <OleCtl.h>
#include <Shlwapi.h>

 

//

 

#pragma comment(lib, "Shlwapi");

HRESULT ShowPicture(LPVOID pImageData, DWORD dwLength, HWND hWnd)
{
 HDC hDC = NULL;
 LPVOID pData = NULL;
 HGLOBAL hGlobal = NULL;
 OLE_XSIZE_HIMETRIC xWidth = 0;
 OLE_YSIZE_HIMETRIC xHeight = 0;

 IStream *pStream = NULL;
 IPicture *pPicture = NULL;

 CRect rect;
 HRESULT hResult = E_FAIL;
 if (pImageData == NULL || dwLength == 0 || !::IsWindow(hWnd)) {
  goto Exit;
 }
 ::GetClientRect(hWnd, &rect);

 hDC = ::GetDC(hWnd);
 if (hDC == NULL) {
  goto Exit;
 }
 hGlobal = ::GlobalAlloc(GMEM_MOVEABLE, dwLength);
 if (hGlobal == NULL) {
  goto Exit;
 }
 pData = ::GlobalLock(hGlobal);
 if (pData == NULL) {
  goto Exit;
 }
 else {
  CopyMemory(pData, pImageData, dwLength);
  GlobalUnlock(hGlobal);
 }

 if (FAILED(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream)) || pStream == NULL) {
  goto Exit;
 }
 if (FAILED(OleLoadPicture(pStream, dwLength, TRUE, IID_IPicture, (LPVOID *) &pPicture)) || pPicture == NULL) {
  goto Exit;
 }

 if (FAILED(pPicture->get_Width(&xWidth))) {
  goto Exit;
 }
 if (FAILED(pPicture->get_Height(&xHeight))) {
  goto Exit;
 }
 if (FAILED(pPicture->Render(hDC, 0, 0, rect.Width(), rect.Height(), 0, xHeight, xWidth, -xHeight, NULL))) {
  goto Exit;
 }

 hResult = S_OK;
Exit:
 if (hDC != NULL) {
  ::ReleaseDC(hWnd, hDC);
  hDC = NULL;
 }
 if (hGlobal != NULL) {
  GlobalFree(hGlobal);
  hGlobal = NULL;
 }
 if (pStream != NULL) {
  pStream->Release();
  pStream = NULL;
 }
 if (pPicture != NULL) {
  pPicture->Release();
  pPicture = NULL;
 }
 return hResult;
}

 

HRESULT ShowPicture(LPCTSTR pszPictureFile, HWND hWnd)
{
 LPVOID pData = NULL;
 HANDLE hFile = NULL;
 HGLOBAL hGlobal = NULL;
 DWORD dwFileSize= 0;
 HRESULT hResult = E_FAIL;

 if (pszPictureFile == NULL || !PathFileExists(pszPictureFile) || !(::IsWindow(hWnd))) {
  goto Exit;
 }

 hFile = ::CreateFile(pszPictureFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 hFile = (hFile == INVALID_HANDLE_VALUE) ? NULL : hFile;
 if (hFile == NULL) {
  goto Exit;
 }
 dwFileSize = ::GetFileSize(hFile, NULL);
 if (dwFileSize == -1) {
  goto Exit;
 }
 hGlobal = ::GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
 if (hGlobal == NULL) {
  goto Exit;
 }
 pData = ::GlobalLock(hGlobal);
 if (pData == NULL) {
  goto Exit;
 }
 else {
  DWORD dwByteRead = 0;
  BOOL bErrFlag = ::ReadFile(hFile, pData, dwFileSize, &dwByteRead, NULL) && (dwByteRead == dwFileSize);
  if (hFile != NULL) {
   CloseHandle(hFile);
   hFile = NULL;
  }
  GlobalUnlock(hGlobal);

  if (!bErrFlag) {
   goto Exit;
  }
 }

 hResult = ShowPicture(pData, dwFileSize, hWnd);
Exit:
 if (hFile != NULL) {
  CloseHandle(hFile);
  hFile = NULL;
 }
 if (hGlobal != NULL) {
  GlobalFree(hGlobal);
  hGlobal = NULL;
 }
 return hResult;
}

 

注:工作中发现 OleLoadPicture 在加载文件内容有错的图像文件时,会阻塞在 OleLoadPicture,这是微软的 BUG,如有可能显示无效的图像文件,请勿使用此法显示图像。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值