How to load BMP image from http URL?

HI,

I have scenario where loading bmp images using the code given. This code is working fine and the filename would be a local or unc file location. I want to load the bmp using the url location. for example in this case the file will be
http://server/test/abc.bmp.

I have tried this url as path and using that method it couldn't load the bmp. Can anybody help how to get the hBitmap object properly read from the url so that I can attach that as per the use given in the code..

Thanks In advance..

hBitmap = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), filename, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE|LR_DEFAULTCOLOR);
 
obj_bmp.Attach(hBitmap);

Open in new window

LVL 1
EpurchaseAsked:
Who is Participating?
 
jkrCommented:
You cannot specify an URL in 'LoadImage()' directly, but using the following as a detours should work:
TCHAR* filename = _T("c:\\tmp\\image.bmp");
 
URLDownloadToFile(NULL,_T("http://server/test/abc.bmp"),filename,0,NULL);
 
hBitmap = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), filename, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE|LR_DEFAULTCOLOR);
 
obj_bmp.Attach(hBitmap);

Open in new window

0
 
EpurchaseAuthor Commented:
Thanks much for your response..Sorry I didn't mention in my previous message that, it is not intended to store in a local file as the bmp image should be something very confidential and should be there in server itself..It will be displayed to the user by checking the given access..

I have seen in one article the use of "URLOpenStream" and tried that..It is found that the onDataAvailable for the callback reference is able to dowload the bmp data to bytes buffer..but the return type of the onDataAvailable method is HRESULT and coule return "S_OK"..Can you please help me if it this way can work for my requirement then, how to read the downloaded data and get it into the HBITMAP object..

Appreciating your help and thanks..

0
 
jkrCommented:
Hm, what avout deleting it afterwards?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
EpurchaseAuthor Commented:
Yes. I agree..Deleting would be fine..But will it be more time consuming incase multiple files to read and attach ? Will it make the process slow?..

Please suggest..
0
 
EpurchaseAuthor Commented:
I missed this concern in my previous message, while copy to client machine and deleting , then need to have access to that specific folder right..then each client need to keep the folder with all access right? or any other way..becoz sometime client restricts the access to delete anyfile from the drive..

Thanks..
0
 
jkrCommented:
Deleting the file will not have any noticeable impact on the performance. The main issue here is that there is simply no API to load a bitmap directly from an URL (keep in mind, these APIs were designed at the early 90s where there was no internet), so I'm afraid you'll have to go that route...
0
 
EpurchaseAuthor Commented:
oh..ok..What about opting URLOpenStream.. How to read the data after executing this method..I have debugged the program after executing the given code and found that onDataAvailable, its getting the data from url ..but how to read that data to HBITMAP..

will this way work, if we can read that data..what do you suggest for this option..

Thanks very much for all your quick responses..

CBSCallbackImpl callback;
	HRESULT hResult = ::URLOpenStream(NULL,bmpFile,0,&callback);
 
STDMETHODIMP CBSCallbackImpl::OnDataAvailable(DWORD, DWORD dwSize,
        FORMATETC *, STGMEDIUM *stgmed )
{
  TRACE(_T("IBindStatusCallback::OnDataAvailable %ld\n"), dwSize );
 
  if ( stgmed ) {
    if ( stgmed->pstm ) {
 
      ULONG bytesRead = 0;
 
      BYTE *data = new BYTE[ dwSize ];
 
      HRESULT hRes = stgmed->pstm->Read( data, dwSize, &bytesRead );
 
      delete[] data;
 
      return hRes;
    }
  }
 
  return S_OK;
}

Open in new window

0
 
jkrCommented:
>>oh..ok..What about opting URLOpenStream..

That does pretty much the same - with the only diofference being that you could store that data in memory instead of on disk. So, if yo udon't

      delete[] data;

youÄll have th ebitmap data in there. Unfortunately, in a base64 encoded format which you can decode to a binary bitmap using http://www.codeproject.com/KB/cpp/base64coding.aspx ("Base64 Encoding And Decoding")
0
 
EpurchaseAuthor Commented:
Hi jkr,

As you said that that the perfomance would be same for both, I am preferring the way you have suggested..

Hence it will save in to one file and then delete from there..

Really appreaciating your help and thanks much..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.