Click here to Skip to main content
15,888,610 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: why does it use so many memory Pin
Randor 5-Jun-10 7:02
professional Randor 5-Jun-10 7:02 
QuestionRe: why does it use so many memory Pin
David Crow7-Jun-10 4:17
David Crow7-Jun-10 4:17 
QuestionHow to Save Image from WebBrowser control Pin
voo doo125-Jun-10 5:23
voo doo125-Jun-10 5:23 
AnswerRe: How to Save Image from WebBrowser control Pin
Hristo-Bojilov5-Jun-10 6:35
Hristo-Bojilov5-Jun-10 6:35 
GeneralRe: How to Save Image from WebBrowser control Pin
voo doo125-Jun-10 7:21
voo doo125-Jun-10 7:21 
GeneralRe: How to Save Image from WebBrowser control Pin
Hristo-Bojilov5-Jun-10 8:14
Hristo-Bojilov5-Jun-10 8:14 
GeneralRe: How to Save Image from WebBrowser control Pin
voo doo125-Jun-10 16:09
voo doo125-Jun-10 16:09 
GeneralRe: How to Save Image from WebBrowser control [modified] Pin
Hristo-Bojilov6-Jun-10 2:57
Hristo-Bojilov6-Jun-10 2:57 
voo doo12 wrote:
Can you show me how I could save image(picture) from webbrowser control using MFC?


Of course I can.

In MFC you should use Doc/View framework and CHtmlView class to access HTML using DOM.
[EDIT]I used Gdi+ to handle the images[/EDIT]
Here is my demo sample:

/*******************************************
 This function cames from MS samples
*******************************************/
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
   UINT  num = 0;          // number of image encoders
   UINT  size = 0;         // size of the image encoder array in bytes

   ImageCodecInfo* pImageCodecInfo = NULL;

   GetImageEncodersSize(&num, &size);
   if(size == 0)
      return -1;  // Failure

   pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
   if(pImageCodecInfo == NULL)
      return -1;  // Failure

   GetImageEncoders(num, size, pImageCodecInfo);

   for(UINT j = 0; j < num; ++j)
   {
      if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
      {
         *pClsid = pImageCodecInfo[j].Clsid;
         free(pImageCodecInfo);
         return j;  // Success
      }    
   }

   free(pImageCodecInfo);
   return -1;  // Failure
}


void CWebBrowserAppView::OnFileSave()
{
	LPDISPATCH pDisp=GetHtmlDocument();

	IHTMLDocument2* pDoc;
	HRESULT hr=0;

        hr=pDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
	if(FAILED(hr))
	{
		pDisp->Release();
		AfxMessageBox(L"Sorry dude, cast failed!");
		return;
	}

	IHTMLElementCollection* pImgs;
	hr=pDoc->get_images(&pImgs);
	if(FAILED(hr))
	{
		pDoc->Release();
	        pDisp->Release();
		AfxMessageBox(L"Sorry dude, can't get images list from document!");
		return;
	}


	long length;
	pImgs->get_length(&length);

	IDispatch *pdImg;
        IHTMLImgElement *pImg;

	CLSID jpgClsid;
	GetEncoderClsid(L"image/jpeg", &jpgClsid);



	for(int i=0;i<length;i++)
	{
		_variant_t index = i;
                hr = pImgs->item( index, index, &pdImg);
                if(SUCCEEDED(hr))
		{
			hr= pdImg->QueryInterface( IID_IHTMLImgElement,  (void **) &pImg);

			 if(SUCCEEDED(hr))
			 {
						long width,height;
						pImg->get_width(&width);
						pImg->get_height(&height);

						IHTMLElementRender* pRend;
						hr=pImg->QueryInterface(IID_IHTMLElementRender,(void**)&pRend);
						if(SUCCEEDED(hr))
						{
								Bitmap* pBmp=new Bitmap(width,height,PixelFormat24bppRGB);

								Graphics* pGr=Graphics::FromImage(pBmp);
								HDC gDc=pGr->GetHDC();
								hr=pRend->DrawToDC(gDc);
								pGr->ReleaseHDC(gDc);


								if(SUCCEEDED(hr))
								{
									CString name;
									name.Format(L"%d.jpg",i);
									pBmp->Save(name,&jpgClsid,NULL);
									name.ReleaseBuffer();
								}

								pRend->Release();
								delete pBmp;
						}
						pImg->Release();
				}

			pdImg->Release();
		}

	}

	pImgs->Release();
	pDoc->Release();
	pDisp->Release();

}

Life is a stage and we are all actors!
modified on Sunday, June 6, 2010 9:14 AM

GeneralRe: How to Save Image from WebBrowser control Pin
voo doo126-Jun-10 4:54
voo doo126-Jun-10 4:54 
GeneralRe: How to Save Image from WebBrowser control Pin
Hristo-Bojilov6-Jun-10 5:06
Hristo-Bojilov6-Jun-10 5:06 
Question0xC0000138: Ordinal Not Found Pin
eusto5-Jun-10 1:51
eusto5-Jun-10 1:51 
AnswerRe: 0xC0000138: Ordinal Not Found Pin
Randor 5-Jun-10 6:28
professional Randor 5-Jun-10 6:28 
GeneralRe: 0xC0000138: Ordinal Not Found Pin
eusto5-Jun-10 7:36
eusto5-Jun-10 7:36 
GeneralRe: 0xC0000138: Ordinal Not Found Pin
Randor 5-Jun-10 7:49
professional Randor 5-Jun-10 7:49 
GeneralRe: 0xC0000138: Ordinal Not Found Pin
eusto5-Jun-10 8:50
eusto5-Jun-10 8:50 
GeneralRe: 0xC0000138: Ordinal Not Found Pin
Randor 5-Jun-10 9:24
professional Randor 5-Jun-10 9:24 
QuestionHow to covert CString to LPCSTR ? Pin
wangningyu4-Jun-10 20:46
wangningyu4-Jun-10 20:46 
AnswerRe: How to covert CString to LPCSTR ? Pin
Richard MacCutchan4-Jun-10 21:19
mveRichard MacCutchan4-Jun-10 21:19 
GeneralRe: How to covert CString to LPCSTR ? Pin
wangningyu4-Jun-10 21:23
wangningyu4-Jun-10 21:23 
GeneralRe: How to covert CString to LPCSTR ? Pin
Richard MacCutchan4-Jun-10 22:11
mveRichard MacCutchan4-Jun-10 22:11 
AnswerRe: How to covert CString to LPCSTR ? Pin
Cedric Moonen4-Jun-10 21:28
Cedric Moonen4-Jun-10 21:28 
AnswerRe: How to covert CString to LPCSTR ? Pin
Aescleal4-Jun-10 23:19
Aescleal4-Jun-10 23:19 
Questionmessin with javascript Pin
csrss4-Jun-10 14:31
csrss4-Jun-10 14:31 
AnswerRe: messin with javascript Pin
Dr.Walt Fair, PE4-Jun-10 15:46
professionalDr.Walt Fair, PE4-Jun-10 15:46 
GeneralRe: messin with javascript Pin
csrss4-Jun-10 16:56
csrss4-Jun-10 16:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.