Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bitmap not loading into Picture control from a resource only DLL

Status
Not open for further replies.

richsb

Technical User
Aug 2, 2001
34
NO
Hi,

As per my previous post I am having trouble with loading resources from a dll. This time it is a Bitmap that I want to grab and place in a picture box

Here's the code - guidance much appreciated.

As you can see I am simply taking the name of a resource only dll, saving the dll name as a CString, calling a handle to the file then using LoadBitmap as per MSDN guidance

This code compiles and runs but doesn't show the image.

Code:
void CDllImageDlg::OnOpenFile() 
{
	CFileDialog dlg(
		true,	//create an open file dialog
		NULL,	//default file extension
		NULL,	//filename
		NULL,   //flags 
		_T("Wreck Data Files (.dll)| *.dll|")		//filter string
		_T("All files (.txt)| *.*|")
		_T("|")
		);

		if (IDOK != dlg.DoModal())  // display open file dialog
			return;		    // do nothing if open file dialog cancel button pressed

		// take selected file and insert file name into a public CString variable for later use
		m_NewDatFile = dlg.GetFileName();

//********************** call the string from the dll

		// create a handle for the dll
		HINSTANCE hDLL;               // Handle to DLL

		// create char buffer 
		char m_CharNewDatFile;

		// create pointer for loadbitmap
		char *lpImageBuffer = &m_CharNewDatFile;

		// call and load the dll
		hDLL = LoadLibrary(m_NewDatFile);

		// create a HBITMAP variable for the chart picture control
		HBITMAP h_ChartBmp;

		// call Loadbitmap function and load bmp for image 
		h_ChartBmp = (HBITMAP) LoadBitmap(hDLL,   
lpImageBuffer	
);

		// load the picture control with the setbitmap function and load 
		// the bitmap into it 
		m_ImageBox.SetBitmap(h_ChartBmp);

			MessageBox("past image load");
}
 
Where do you actually name the resource to be loaded? The second parameter in the call to LoadBitmap() is not a receiving buffer; it should contain the name or the resourceid of the bitmap to be loaded.

I suggest you give the bitmap a resourceid instead of a name and use the MAKEINTRESOURCE macro to identify it.

Furthermore; LoadBitmap is sort of obsolete; it has been superseeded by LoadImage(). Ah.... and the bitmap needs to be placed in the correct section of the resource fie, of course....


I always create COM objects of the bitmaps and stuff; they're rather easy to manipulate, render, resize etc.

Something like this:

Code:
STDMETHODIMP CResourceHandler::LoadBitmap(long nID, VARIANT_BOOL bTransparentBackground, IPictureDisp **ppIBitmap)
//Loads the bitmap with the specified ID from the resource dll.
{
	LOCK(&m_csVars);

	HRESULT hRes=S_OK;

	//Check parameters:
	if(ppIBitmap==NULL)
		hRes = g_pServer->CreateError(CLSID_ResourceHandler, IID_IResourceHandler, E_POINTER, _T("An invalid pointer has been specified."), m_pSysHandler);

	else if(nID<1)
		hRes = g_pServer->CreateError(CLSID_ResourceHandler, IID_IResourceHandler, E_INVALIDARG, _T("An invalid argument has been specified."), m_pSysHandler);

	else {
		//Load the bitmap:
		HBITMAP hBmp=m_pRes->LoadResBitmap(nID, static_cast<BOOL>(bTransparentBackground));
			
		if(hBmp!=NULL) {
			//And create an OLE picture object:
			PICTDESC picDesc={0};

			picDesc.cbSizeofstruct = sizeof(PICTDESC);
			picDesc.picType = PICTYPE_BITMAP;
			picDesc.bmp.hbitmap = hBmp;
			picDesc.bmp.hpal = 0;

			if(OleCreatePictureIndirect(&picDesc, IID_IPictureDisp, TRUE, reinterpret_cast<void**>(ppIBitmap))!=S_OK)
				hRes = hRes = g_pServer->CreateError(CLSID_ResourceHandler, IID_IResourceHandler, lsdResE_ImageNotLoaded, _T("The image could not be loaded."), m_pInternalHandler);

		}else
			hRes = g_pServer->CreateError(CLSID_ResourceHandler, IID_IResourceHandler, lsdResE_ImageNotFound, _T("The specified image could not be found in the resource file."), m_pInternalHandler);
	}

	UNLOCK(&m_csVars);

	return hRes;
}

HBITMAP CResourceReader::LoadResBitmap(UINT uID, BOOL bTransparent)
{
	return static_cast<HBITMAP>(LoadImage(static_cast<HINSTANCE>(GetLockedVar(&m_csVars, m_hLib)), MAKEINTRESOURCE(uID), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR|(bTransparent ? LR_LOADTRANSPARENT : 0)));
}

If ever you need the handle to the bitmap; you can obtain that from the IPicture or IPicureDisp pointers just as well....

Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top