如何将HTML转换为图像? [英] How to convert HTML to image?

查看:97
本文介绍了如何将HTML转换为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试实现的功能类似于Firefox的Screengrab插件.我找到了一些链接,但大多数都需要我

1.将页面另存为HTML/MHTML并将其转换为图片->无法执行此操作,因为它会导致CSS样式松动
2.传入页面的URL以转换为图片->无法在需要密码的网站中使用.

它不必只在Firefox上运行.我只需要能够在Firefox或IE上做到这一点. :sigh:

请帮忙.

Hi,

The feature i''m trying to achieve is something like Firefox''s Screengrab Add-On. I''ve found some links but most of them required me to either

1. Save the page as HTML/MHTML and convert it to image -> Can''t do this because it''ll loose the CSS style
2. Pass in the URL of the page to convert to image -> Won''t work in password required sites.

It doesn''t have to only run on Firefox. I just need to be able to do it either on Firefox or IE. :sigh:

Please help.

推荐答案

我是用这种方式做到的.

I do it in this way.

IViewObject2*          view = 0;
IWebBrowser2*          wbb = 0;
unsigned int          connid = 0;
IOleObject*            oleo = 0;
IOleInPlaceObject*    oipo = 0;
iOleClientSite*        iolesite = 0;
RECT                  rc = {0,0,100,100};
MSG                    msg; memset(&msg,0,sizeof(msg));

Empty();
iolesite = new iOleClientSite; if(!iolesite) return E_OUTOFMEMORY;

if(S_OK==(hr=CoCreateInstance(IID_IInternetExplorer,0,CLSCTX_INPROC_SERVER,IID_IWebBrowser2,(void**)&wbb)))
{
  if(S_OK==wbb->QueryInterface(IID_IOleObject,(void**)&oleo))
  {
    oleo->SetClientSite(iolesite);
    connid = ConnectSink(oleo,IID_DWebBrowserEvents2,(bUnk*)iolesite);
    oleo->DoVerb(OLEIVERB_INPLACEACTIVATE,&msg,iolesite,0,iolesite->_hwnd,&rc);
    oleo->Release(); oleo = 0;

    iolesite->Resize(wbb,_sil.cx,_sil.cy);
  }

  {
    tArr<unsigned short>    wstr(8+strlen(file)); MultiByteToWideChar(0,0,file,1+strlen(file),wstr.array,wstr.count);
    VARIANT                  v; v.vt = VT_BSTR; v.bstrVal = SysAllocString(wstr.array);
    VARIANT                  i; i.vt = VT_INT; i.lVal = 0;
    READYSTATE              ready;

    hr = wbb->Navigate2(&v,&i,0,0,0);

    while((S_OK==wbb->get_ReadyState(&ready)) && (READYSTATE_COMPLETE!=ready) && GetMessage(&msg,0,0,0))
    {
      DispatchMessage(&msg);
    }

    VariantClear(&v);
    VariantClear(&i);
  }

  IDispatch*    app = 0;
  if((S_OK==hr) && (S_OK==wbb->get_Document(&app)) )
  {
    if(app)
    {
      _Object    obj(app,1);
      int        cxy = 0;
      int        cxr = 0; // rahmen kann nicht ermittelt werden
      int        cyr = 0;


      if(obj["body"].get("scrollHeight",cxy)) _sil.cy = cxy+cyr;
      if(obj["body"].get("scrollWidth" ,cxy)) _sil.cx = cxy+cxr;

      TRACE("document.body.scrollWidth = %i\r\ndocument.body.scrollHeight = %i\r\n",_sil.cx,_sil.cy);

      iolesite->Resize(wbb,_sil.cx,_sil.cy);

      if(obj["body"].get("scrollHeight",cxy)) _sil.cy = cxy+cxr;

      if(forcecx) _sil.cx = forcecx;

      iolesite->Resize(wbb,_sil.cx,_sil.cy);

      if(S_OK==app->QueryInterface(IID_IViewObject2,(void**)&view))
      {
        HDC                hdcm = CreateCompatibleDC(0);
        RECTL              rcbound = { 0,0,_sil.cx,_sil.cy };
        HGDIOBJ            obmp;
        BITMAPINFO        bmi;
        unsigned int      bpl;

        if(forcecy) _sil.cy = min(_sil.cy,forcecy);

        bmi.bmiHeader.biSize          = sizeof(bmi);
        bmi.bmiHeader.biWidth         = _sil.cx;
        bmi.bmiHeader.biHeight        = _sil.cy;
        bmi.bmiHeader.biPlanes        = 1;
        bmi.bmiHeader.biBitCount      = 24;
        bmi.bmiHeader.biCompression   = 0;

        bpl = (((bmi.bmiHeader.biWidth*bmi.bmiHeader.biBitCount)>>3)+3)&~3;
        bmi.bmiHeader.biSizeImage     = bmi.bmiHeader.biHeight*bpl;
        bmi.bmiHeader.biXPelsPerMeter = 0;
        bmi.bmiHeader.biYPelsPerMeter = 0;
        bmi.bmiHeader.biClrUsed       = 0;
        bmi.bmiHeader.biClrImportant  = 0;

        _hbmp = CreateDIBSection(hdcm,&bmi,DIB_RGB_COLORS,0,0,0);

        obmp = SelectObject(hdcm,_hbmp);
        hr = view->Draw(DVASPECT_CONTENT,-1,0,0,0,hdcm,&rcbound,0,__fnContDraw,0);
        SelectObject(hdcm,obmp);

        DeleteDC(hdcm);
        view->Release(); view = 0;
      }
      app->Release(); app = 0;
    }
  }



  if(S_OK==wbb->QueryInterface(IID_IOleObject,(void**)&oleo))
  {
    DisconnectSink(oleo,IID_DWebBrowserEvents2,(bUnk*)iolesite,connid);
    oleo->SetClientSite(0);
    oleo->Release(); oleo = 0;
  }

  wbb->Release();
}
iolesite->Release();


对于Internet Explorer,您可以创建浏览器帮助程序对象(BHO). BHO是Internet Explorer的插件.它必须是一个好的开始.

您可以执行类似于 http://h71028.www7.hp的操作. com/hho/cache/482779-0-0-225-121.html [
For Internet explorer , you can create browser helper object(BHO). BHO are plugins for internet explorer.It must be a good start.

You can do something similar to http://h71028.www7.hp.com/hho/cache/482779-0-0-225-121.html[^] with BHO.


这篇关于如何将HTML转换为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆