How to Save Image from WebBrowser control
-
Hi everybody. I'm tring to save image form webBrowser control. and i have the following code
mshtml::HTMLDocument^ document = dynamic_castmshtml::HTMLDocument^(this->webBrowser1->Document->DomDocument);
mshtml::IHTMLElementCollection^ collImages = document->getElementsByTagName(L"img");for (int i = 0; i < collImages->length; ++i)
{
mshtml::IHTMLImgElement^ img = safe_castmshtml::IHTMLImgElement^(collImages->item(nullptr, i));
mshtml::IHTMLElementRender^ render = dynamic_castmshtml::IHTMLElementRender^(img);Bitmap^ bmp = gcnew Bitmap(img->width, img->height);
Graphics^ g = Graphics::FromImage(bmp);
IntPtr hdc = g->GetHdc();
render->DrawToDC(hdc); //Here is the Error
g->ReleaseHdc(hdc);
delete g; g = nullptr;
bmp->Save(L"C:\\Test\\SaveImage.Jpg", System::Drawing::Imaging::ImageFormat::Jpeg);
}I get the following error message Error C2664: 'mshtml::IHTMLElementRender::DrawToDC' : cannot convert parameter 1 from 'System::IntPtr' to 'mshtml::_RemotableHandle %' is there any one who knows how to fix this?
-
Hi everybody. I'm tring to save image form webBrowser control. and i have the following code
mshtml::HTMLDocument^ document = dynamic_castmshtml::HTMLDocument^(this->webBrowser1->Document->DomDocument);
mshtml::IHTMLElementCollection^ collImages = document->getElementsByTagName(L"img");for (int i = 0; i < collImages->length; ++i)
{
mshtml::IHTMLImgElement^ img = safe_castmshtml::IHTMLImgElement^(collImages->item(nullptr, i));
mshtml::IHTMLElementRender^ render = dynamic_castmshtml::IHTMLElementRender^(img);Bitmap^ bmp = gcnew Bitmap(img->width, img->height);
Graphics^ g = Graphics::FromImage(bmp);
IntPtr hdc = g->GetHdc();
render->DrawToDC(hdc); //Here is the Error
g->ReleaseHdc(hdc);
delete g; g = nullptr;
bmp->Save(L"C:\\Test\\SaveImage.Jpg", System::Drawing::Imaging::ImageFormat::Jpeg);
}I get the following error message Error C2664: 'mshtml::IHTMLElementRender::DrawToDC' : cannot convert parameter 1 from 'System::IntPtr' to 'mshtml::_RemotableHandle %' is there any one who knows how to fix this?
-
Wrong forum.You should post you question to C++/CLI forum.
Life is a stage and we are all actors!
-
voo doo12 wrote:
I did. But no one seems to know.
OK. I would advice you to write your own managed wrapper for of IHTMLElementRender interface, never mix managed and unmaged code, and avoid using C++/CLI for anything- it's not stable enough, it's very slow and inefficient, it's dirty.You are supposed to use C# or VB.NET, when you are targeting .NET platform. If you need to create something using C++ you should better use MFC libraries.Good luck.
Life is a stage and we are all actors!
-
voo doo12 wrote:
I did. But no one seems to know.
OK. I would advice you to write your own managed wrapper for of IHTMLElementRender interface, never mix managed and unmaged code, and avoid using C++/CLI for anything- it's not stable enough, it's very slow and inefficient, it's dirty.You are supposed to use C# or VB.NET, when you are targeting .NET platform. If you need to create something using C++ you should better use MFC libraries.Good luck.
Life is a stage and we are all actors!
-
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 bytesImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // FailurepImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // FailureGetImageEncoders(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)) { Bitm
-
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 bytesImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // FailurepImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // FailureGetImageEncoders(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)) { Bitm