admin';?>

首页 / Windows

Load jpeg, png, gif, bmp from file or resource (support xp,win7+ )

By admin •  2019-04-23 14:02:43 •  71次点击
1. Load image from file
 //https://www.experts-exchange.com/questions/20561165/Convert-a-jpeg-to-HBITMAP.html
 HBITMAP ReadJpegFile(LPCTSTR pFile)
 {
  HANDLE hFile = 0;
  HBITMAP hBmp = 0;
  DWORD dwSize = 0;
  DWORD dwRead = 0;
  HGLOBAL hMemJpeg = 0;
  LPSTREAM lpStream = 0;
  OLE_HANDLE hJpegBmp = 0;
  HRESULT hr = 0;
  LPPICTURE lpPicture = NULL;
  void *pMemJpeg = 0;

  // Open the file and get the size.
  if ((hFile = CreateFile(pFile, GENERIC_READ, FILE_SHARE_READ,
   NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
   return NULL;

  do {
   if ((dwSize = GetFileSize(hFile, NULL)) == 0xFFFFFFFF)
   {
    break;
   }

   // Allocate space for file, read it in, and then close the file again. 
   if ((hMemJpeg = GlobalAlloc(GMEM_MOVEABLE, dwSize)) == NULL)
   {
    break;
   }

   if ((pMemJpeg = GlobalLock(hMemJpeg)) == NULL)
   {
    break;
   }

   if (!ReadFile(hFile, pMemJpeg, dwSize, &dwRead, NULL))
   {
    GlobalUnlock(hMemJpeg);
    break;
   }
   CloseHandle(hFile); hFile = 0;
   GlobalUnlock(hMemJpeg);

   // Create the stream and load the picture. 
   if ((hr = CreateStreamOnHGlobal(hMemJpeg, TRUE, &lpStream)) != S_OK)
   {
    break;
   }

   if (OleLoadPicture(lpStream, dwSize, FALSE, IID_IPicture, (LPVOID*)&lpPicture) != S_OK)
   {
    break;
   }

   // Get the handle to the image, and then copy it.
   if ((lpPicture->get_Handle(&hJpegBmp)) != S_OK)
   {
    break;
   }

   if ((hBmp = (HBITMAP)CopyImage((HANDLE *)hJpegBmp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)) == NULL)
   {
    break;
   }
  } while (0);

  if (lpPicture) {
   lpPicture->Release();
  }

  if (lpStream) {
   lpStream->Release();
  }

  if (hMemJpeg) {
   GlobalFree(hMemJpeg);
  }

  if (hFile) {
   CloseHandle(hFile);
  }
  return hBmp;
 } // End of ReadJpegFile(). 

2.Load image from resource.rc
 //https://social.msdn.microsoft.com/Forums/en-US/c8dea6f9-0564-49d6-9782-117207031140/loadfromresource-fails-for-loading-png-files-from-resource?forum=vcmfcatl
 HBITMAP ReadJpegFromRes(IN const TCHAR* nResId, IN const TCHAR* nResType, HINSTANCE hInst /*= GetModuleHandle(0)*/)
 {
  HBITMAP hBmp = 0;

  HRSRC hRes = FindResource(hInst, nResId, nResType);
  if (!hRes) return 0;

  DWORD rsize = SizeofResource(hInst, hRes);
  const void* pResourceData = (void*)LockResource(::LoadResource(hInst, hRes));

  LPPICTURE lpPicture;
  OLE_HANDLE picHandle = 0;
  do {
   if (!pResourceData) break;

   HGLOBAL  hGlobal = ::GlobalAlloc(GMEM_MOVEABLE, rsize);
   if (!hGlobal) break;

   void* pBuffer = ::GlobalLock(hGlobal);
   memcpy(pBuffer, pResourceData, rsize);
   LPSTREAM lpStream = NULL;

   // create IStream* from global memory
   HRESULT hr = CreateStreamOnHGlobal(pBuffer, TRUE, &lpStream);
   if (hr != S_OK) {
    ::GlobalUnlock(pBuffer);
    ::GlobalFree(hGlobal);
    break;
   }
   ::GlobalUnlock(pBuffer);
   hr = ::OleLoadPicture(lpStream,
    0,
    FALSE,
    IID_IPicture,
    (LPVOID *)&lpPicture);
   lpStream->Release();

   if (hr != S_OK) {
    ::GlobalFree(hGlobal);
    break;
   }

   hr = lpPicture->get_Handle(&picHandle);
   ::GlobalFree(hGlobal);

   hBmp = (HBITMAP)picHandle;
  } while (0);

  return hBmp;
 }
in your resource.rc, you can define:
IDR_YOUR            YOUR_JPG                 "D:\proj\ATLProj\PacingWolf\small_bg.jpg"
, then you can call it as following:
HBITMAP hbitmap = nsYLX::ReadJpegFromRes(MAKEINTRESOURCE(IDR_YOUR), _T("YOUR_JPG"), GetModuleHandle(0));
//....do some fun
::DeleteObject(hbitmap);

if you can't compile them, you may need add some .h files:
#include <windows.h>
#include <tchar.h>
#include <atlbase.h>
#include <atlexcept.h>
#include <atlconv.h>
#include <atlapp.h> 
#include <atltypes.h>
#include <atlctrls.h>

more useful functions about image, you can find from
http://yaolixing.oltag.com/gns-9E5AC4E1-AFB8-465D-95CE-5A38A4926420.html

0 回复 | 直到2019-06-26 15:39添加回复

回复

登录发表 or 还没有账号?去注册

他山跨平台混合应用开发框架

现在注册 已注册请 登入

节点列表