I'm creating a program, and I need to load a PNG image from the resource files. The goal is simple, pass a Bitmap object by reference to a function, which assigns the PNG from the stream to it, and returns. Because it's by reference, the origial should be affected.
Using the tutorial over on CodeProject, I came up with the following code:
Code:
int loadResource(Bitmap& image,LPCTSTR name,LPCTSTR type,HINSTANCE hinst=GetModuleHandle(0)){
HRSRC hRes=FindResource(hinst, name, type);
if(!hRes) return -1;
unsigned long imgSize=SizeofResource(hinst, hRes);
if(!imgSize) return -1;
const void* pResData=LockResource(LoadResource(hinst, hRes));
if(!pResData) return -1;
HGLOBAL buffer=GlobalAlloc(GMEM_MOVEABLE, imgSize);
if(buffer){
void* pBuffer=GlobalLock(buffer);
if(pBuffer){
CopyMemory(pBuffer, pResData, imgSize);
IStream* pStream=NULL;
if(CreateStreamOnHGlobal(buffer, false, &pStream)==S_OK){
image.FromStream(pStream);
int status=image.GetLastStatus();
if(status==Ok){
pStream->Release();
GlobalUnlock(buffer);
GlobalFree(buffer);
return status;
}
else{
pStream->Release();
GlobalUnlock(buffer);
GlobalFree(buffer);
return status;
}
}
}
}
return -1;
}
And in my WM_PAINT:
Code:
/*Standard stuff*/
HINSTANCE hinst=GetModuleHandle(0);
//This one works, but in the release, I don't want the image outside of the EXE
//Bitmap image(L"temp.png");
Bitmap image(30,44);
loadResource(image, MAKEINTRESOURCE(IDB_TEMP), RT_RCDATA);
Graphics graphics(hdc);
TextureBrush brush(&image,WrapModeTile);
graphics.FillRectangle(&brush, Rect(0, 0, 60, 88));
/*Standard stuff*/
The problem is that the original Bitmap isn't updated properly. It's initialized, and keeps that value, even after loadResource() runs.
Can anybody help me out with this?