Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to Save Image from WebBrowser control

How to Save Image from WebBrowser control

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelptutorialquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    voo doo12
    wrote on last edited by
    #1

    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?

    L 1 Reply Last reply
    0
    • V voo doo12

      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?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Wrong forum.You should post you question to C++/CLI forum.

      Life is a stage and we are all actors!

      V 1 Reply Last reply
      0
      • L Lost User

        Wrong forum.You should post you question to C++/CLI forum.

        Life is a stage and we are all actors!

        V Offline
        V Offline
        voo doo12
        wrote on last edited by
        #3

        I did. But no one seems to know.

        L 1 Reply Last reply
        0
        • V voo doo12

          I did. But no one seems to know.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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!

          V 1 Reply Last reply
          0
          • L Lost User

            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!

            V Offline
            V Offline
            voo doo12
            wrote on last edited by
            #5

            Can you show me how I could save image(picture) from webbrowser control using MFC?

            L 1 Reply Last reply
            0
            • V voo doo12

              Can you show me how I could save image(picture) from webbrowser control using MFC?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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))
              					{
              							Bitm
              
              V 1 Reply Last reply
              0
              • L Lost User

                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))
                					{
                							Bitm
                
                V Offline
                V Offline
                voo doo12
                wrote on last edited by
                #7

                Thanks very much. I'm not familiar with MFC but I will be. Thanks again

                L 1 Reply Last reply
                0
                • V voo doo12

                  Thanks very much. I'm not familiar with MFC but I will be. Thanks again

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You are always wellcome.Study hard and you will get results.

                  Life is a stage and we are all actors!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups