Click to See Complete Forum and Search --> : Access Violation with Gdiplus::Bitmap constructor


Thales Medeiros
June 4th, 2006, 02:58 AM
Hi, all!

I read a post here about Access Violation with GDI+, and the problem had been solved when the user simply called the GdiplusStartup() function. In my case this function is already being called, and the problem is a little different.

I created a class that Load JPG files from the executable resource. The class is something like this:

class TJPGResource {
public:
TJPGResource() {}
virtual ~TJPGResource() {}

bool Load(UINT ResourceID, LPCTSTR ResourceType);
Gdiplus::Bitmap *GetBitmap() {return pBitmap;}
private:
Gdiplus::Bitmap *pBitmap;
};

bool TJPGResource::Load(UINT ResourceID, LPCTSTR ResourceType) {
/*
Some code here to load the image data to the variable hBuffer
*/

IStream *pStream;
if(CreateStreamOnHGlobal(hBuffer, FALSE, &pStream)==S_OK) {

//************************************************
pBitmap=new Gdiplus::Bitmap(pStream);
//Here the program crashes with an Access Violation message
//************************************************

//************************************************
Gdiplus::Bitmap *localBitmap=new Gdiplus::Bitmap(pStream);
//This code works OK in place of the original above, but that's not what I want
//************************************************

}
}

Doesn't need to say that I'm using GdiplusStartup(), and of course the "using namespace Gdiplus;" is in the code.

For me it's not a GDI+ problem. In fact, when I try to put any value to the pBitmap variable, like point it to NULL, the program crashes too. I've already put this member as private, protected and public. In all cases the problem is the same.

Thanks for the attention of all! Hope u can help me with this headache.


Thales Medeiros.

ovidiucucu
June 6th, 2006, 08:24 AM
/*
Some code here to load the image data to the variable hBuffer
*/
Who knows, maybe exactly "some code..." has some problems.

Anyhow here is an example that works:
class CGDIPBitmap
{
public:
CGDIPBitmap() : m_pBitmap(NULL) {}
// Attributes
public:
Bitmap* m_pBitmap;
// Operations
void LoadFromResource(UINT nResID, LPCTSTR pszResType);
// ...
// Implementation
protected:
void SafeDelete();
// ...
};
void CGDIPBitmap::SafeDelete()
{
if(NULL != m_pBitmap)
{
delete m_pBitmap;
m_pBitmap = NULL;
}
}
bool CGDIPBitmap::LoadFromResource(UINT nResID, LPCTSTR pszResType)
{
SafeDelete();

HINSTANCE hInstance = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource(hInstance, MAKEINTRESOURCE(nResID), pszResType);

DWORD dwSize = ::SizeofResource(hInstance, hRsrc);

HGLOBAL hRes = ::LoadResource(hInstance, hRsrc);
const void* pData = ::LockResource(hRes);

HGLOBAL hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, dwSize);
void* pBuffer = ::GlobalLock(hBuffer);

CopyMemory(pBuffer, pData, dwSize);

IStream* pStream = NULL;
HRESULT hr = ::CreateStreamOnHGlobal(hBuffer, FALSE, &pStream);

m_pBitmap = Bitmap::FromStream(pStream);
pStream->Release();

::GlobalUnlock(hBuffer);
::GlobalFree(hBuffer);

Status status = m_pBitmap->GetLastStatus();
return (Ok == status);
}