jpg in resource to byte array

Hi,

Does anyone know how to get an array of bytes from a jpeg-image from resource without the use of GDI+ ?

SFML will do this for you. I think SDL has the ability as well but don't quote me on that, it's been awhile since I looked at that library.
Like any other binary resources: FindResource, LoadResource, LockResource, SizeofResource APIs are exactly what you want.

A sample code can be found here:
http://www.codeproject.com/Articles/4221/Adding-and-extracting-binary-resources
You think this will do the trick:


bool LoadImageFromResource( CImage * pimage, LPCTSTR lpszResourceName, LPCTSTR lpszResourceType)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())

HRSRC hRsrc = ::FindResource(gHINSTANCE, lpszResourceName, lpszResourceType );
if (hRsrc == NULL)
{
return false;
}

HGLOBAL hGlobal = LoadResource(gHINSTANCE, hRsrc);
if (hGlobal == NULL)
{
return false;
}

LPBYTE lpBuffer = (LPBYTE) ::LockResource(hGlobal);
if (lpBuffer == NULL)
{
FreeResource(hGlobal);
return false;
}

bool bRes = false;
{
UINT uiSize = ::SizeofResource(gHINSTANCE, hRsrc);

HGLOBAL hRes = ::GlobalAlloc(GMEM_MOVEABLE, uiSize);
if (hRes != NULL)
{
IStream* pStream = NULL;
LPVOID lpResBuffer = ::GlobalLock(hRes);
ASSERT (lpResBuffer != NULL);

memcpy(lpResBuffer, lpBuffer, uiSize);

HRESULT hResult = ::CreateStreamOnHGlobal(hRes, TRUE, &pStream);

if( hResult == S_OK)
{
pimage->Load(pStream);
pStream->Release();
bRes= true;
}
}
}

UnlockResource(hGlobal);
FreeResource(hGlobal);

return bRes;
}


And then use it by:

CImage image;

if (LoadImageFromResource( &logoImage, MAKEINTRESOURCE(IDJ_STAMP_LOGO), _T("JPG")))
{
char* strImage = (char*)image.GetBits();
}

I don't know if this works correctly, because strImage is a string consisting of only ΓΏ-characters.

What am I doing wrong here?
Topic archived. No new replies allowed.